Free checks to see if the instance is nil before freeing the object so
Dennis's method works fine assuming you initialize the variables to nil
first and don't do anything crazy like override the free method.

procedure Free;

Description

Use Free to destroy an object. Free automatically calls the destructor
if the object reference is not nil. Any object instantiated at runtime
that does not have an Owner should be destroyed by a call to Free, so
that can be properly destroyed and the memory released. Unlike Destroy,
Free is successful even if the object is nil, so if the object was never
initialized, Free won't result in an error.

Also FreeAndNil() should be called if the variables scope is greater
than the procedure.

Guy

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Conor Boyd
Sent: Friday, 21 July 2006 2:53 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: RE: [DUG] Related try..finally question

But all your object creation shouldn't really be inside the try's for a
start.

When you call a constructor, and there's an exception inside the
constructor, you're guaranteed that Delphi will tidy up the partially
created instance.

It gets a bit verbose, but IMHO the correct form is as follows:
 
Something := TSomething.Create(nil);
Try
        X := TX.Create(blah); //for some reason crashes here
        Try
                Y := TY.Create();
                Try
                        //Do Something
                Finally
                   FreeAndNil(y); //will throw exception (?) because was
never created
                End;
        Finally
                FreeAndNil(X);
        End;
finally
   FreeAndNil(Something);
End;

Don't get me wrong, I don't often go too far down this route, preferring
instead to refactor into smaller methods for a start.

Dennis' is an interesting suggestion, tho'.  I will look into that.

Cheers,

C.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of James Sugrue

That's an interesting one. I guess you're talking about something like
this?

Try
   Something := TSomething.Create(nil);
   X := TX.Create(blah); //for some reason crashes here
   Y := TY.Create();
Finally
   FreeAndNil(Something);
   FreeAndNil(X);
   FreeAndNil(y); //will throw exception (?) because was never created
End;

If so then you could just

If (something <> nil) then
   FreeAndNil(something);

??

(apologies for any coding errors, haven't done much Delphi recently)

_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi


CONFIDENTIALITY: This email (including any attachments) may contain 
confidential, proprietary and privileged information, and unauthorised 
disclosure or use is prohibited.  If you received this email in error, please 
notify the sender and delete this email from your system.


_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to