×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

What are the uses of freeandnil

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?


Steve
Be excellent to each other and Party on!

RE: What are the uses of freeandnil

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

RE: What are the uses of freeandnil

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

RE: What are the uses of freeandnil

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"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

You're right Clive.  There is no IsAssigned method.  

Andrew IsMuddled
Hampshire, UK

RE: What are the uses of freeandnil

So you've been lying to me about your surname!!!

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close