FindAllFiles() is always create instance of TStringList as the result. NIL means memory address not valid or not allocated and accessing the nil will raise a violation access warning.
I think you should carefully using the FindAllFiles() routine.
TStringlist is never create itself. It's an object and must ALWAYS created into memory.
procedure foo;
var sl : TStringlist;
begin
sl := TStringlist.create;
try
try
sl.add('RTFM');
except
showmessage('error');
end;
finally
sl.free;
end;
end;
In this case, the function give s Tstringlist back. But this is also an object and must be freed (http://wiki.freepascal.org/findallfiles)
I remember to tell this exactly on a dutch forum
TStringlist is never create itself - thats correct. In this case the StringList is created by FindAllFiles.
Have a look at the code:function FindAllFiles(const SearchPath: String; SearchMask: String;
SearchSubDirs: Boolean): TStringList;
var
Searcher: TListFileSearcher;
begin
Result := TStringList.Create;
...So you need not create "Form1.ImageList" before, it produces only a memoryleak.
@ userx-bw:
In such a case I would create "Form1.ImageList" by Form1.OnCreate (now you have a empty StringList, but always useable/assigned) and free it by Form1.OnClose. So you never have to test, its created.
In your procedure, where you fill "Form1.ImageList", use simply a new StringList, what you can free, after assigning it to your ImageList.
e.G.: procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
SL:=FindAllFiles('', '*.*', True);
ImageList.Assign(SL);
SL.Free;
end;
Now that this has turned into a two topic discussion:
Sorry for the poor sentence structure, when I wrote,
the TStringList creates itself
What was meant by what I said is that I myself do not create the Object, but the people that wrote FREE PASCAL, by there code it is created, therefore following the rule, the programmer that creates the object has the responsibility to free it.
Therefore who ever wrote FREE PASCAL, and wrote that function FindAllFiles to create the TStringList now have the responsibility to free that Object that they called to be created by what ever means necessary.
If they, the programmers that wrote the function FindAllFiles, if they themselves have not done this then they have violated there own rules, and the Programming Language is therefore not written correctly, in freeing that Object that was created by there own code that they wrote not I, or another user of this programming language.
It should then have been written to do as follows.
When you call the function
FindAllFiles it has to check to see if an hinstance of the Object TStringList has been created first if NOT then throw error the variable name for TStringList Not Created.
Just like I am trying to find out how to do.
Because what I am checking for is:
If the user of this program sets the working directory or NOT before he clicks the button to execute the program, if he does not set the directory that he wants to use first before he clicks the other button to execute this program that then executes the external program then show a message to tell him that he forgot to set a directory to work out of.
It is because FREE PACAL uses the Function FindAllFIiles to create the TStringList then fills it, the responsibility to free the List is no longer mine, but the people that wrote FREE PASCAL therefore leaving me with just having to check to be sure it got filled, not created.
To know if the user set a directory or not. One would be to see if the Object was created, two see if it has anything in it would this not be the way to do this?
By my code when the user sets the directory it calls FindAllFIles the Object is created then filled by that function.
Therefore all I was doing is trying one of the ways to check to see if the user set the working directory first, because if the user clicks the button I use this program to execute the external program before setting the working directory it is now trying to use an Object that has not yet been created or filled because the function FindAllFiles was never called to create the Object then fill it.
In any stage of the code that is used to create an Object there needs to be code to be sure that is has been created just in case it does not get created for what ever reason. How else does the compiler or Lazarus IDE know if an Object has been created or not to give you or me an error when you are in the developing stages using there IDE when working with an Object?
going back to my reason for writing this question, What is the best way to check to see if a TStringList has even been created so I can check to see if the user even set a working directory so I can stop this program from calling the external program to execute?
Even if I create an Object myself then how do I check to be sure that it in fact it has been created before I try to use it?