What are the uses of freeandnil
What are the uses of freeandnil
(OP)
The freeandnil procedure was introduced after D4, but I have the code for it.
It seems to de-reference the pointer to an object and free it.
Is this sufficient to properly dispose of a populated Tstringlist?
It seems to de-reference the pointer to an object and free it.
Is this sufficient to properly dispose of a populated Tstringlist?
Steve
Be excellent to each other and Party on!
RE: What are the uses of freeandnil
The merit of using FreeAndNil is that you can use IsAssigned on the object reference.
In the following code example, by calling FreeAndNil (test) instead of test.Free, IsAssigned (test) gives the expected answer of FALSE. However, if test.Free was called instead of FreeAndNil then IsAssigned (test) gives the unexpected answer of TRUE and an Access Violation occurs in the ShowMessage.
CODE
var
test: TStringList;
begin
test := TStringList.Create;
try
test.Add( '1' );
test.Add( '2' );
finally
FreeAndNil ( test );
end;
if Assigned ( test ) then
ShowMessage ( test[1] );
end;
Andrew
Hampshire, UK
RE: What are the uses of freeandnil
CODE
Item := NIL;
Yes, it is enough to dispose of a TStringList unless you have used the objects property of the string list to store something. In that case, you have to iterate through the string list and destroy each object and then use FreeAndNil on the string list.
-Dell
-Dell
RE: What are the uses of freeandnil
Clive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: FAQ102-5096
RE: What are the uses of freeandnil
Andrew IsMuddled
Hampshire, UK
RE: What are the uses of freeandnil
Clive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: FAQ102-5096