Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What are the uses of freeandnil

Status
Not open for further replies.

sggaunt

Programmer
Jul 4, 2001
8,620
GB
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?




Steve
Be excellent to each other and Party on!
 
FreeAndNil properly disposes of a (populated) TStringList.

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:
procedure TForm1.Button1Click(Sender: TObject);
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
 
In general, I prefer using FreeAndNil to .Free. Before D4, if there was any chance that you would access an object after it had been freed, you also had to explicitly set it to NIL.
Code:
Item.Free;
Item := NIL;
If you didn't do this, you could not tell if the object was already instantiated or not so you risked an access violation error if you tried to access the object after it had been freed. With FreeAndNil, you don't have to remember to do this in your code because it's handled for you.

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
 
I concur with Andrew, and use FreeAndNil in conjunction with the Assigned method. By the way, there is no IsAssigned method. I think Andrew has just muddled up the name as he refers to Assigned in his code snippet.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"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
 
You're right Clive. There is no IsAssigned method.



Andrew IsMuddled
Hampshire, UK
 
So you've been lying to me about your surname!!! ;-)

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top