You still need to worry about "loiterers" though.

eg. Object A refers to object B, which holds a reference back to object A. Unless one of those references is explictly set to null, before objectA or object B goes out of scope, the objects are never garbage collected....a kind of memory leak!

----- Original Message ----- From: "James Sugrue" <[EMAIL PROTECTED]>
To: "'NZ Borland Developers Group - Delphi List'" <[email protected]>
Sent: Friday, July 21, 2006 3:13 PM
Subject: RE: [DUG] Related try..finally question


Not that it applies too much to this example, but this is why I like C# and
the GC. Don't really have to worry about freeing objects. Only thing you
need to be concerned with is closing data connections and files.

Also,
try
catch
finally

is a better mechanism than
try
 try
 except
finally

IMHO.

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

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



--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.10.1/389 - Release Date: 14/07/2006





--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.10.1/389 - Release Date: 14/07/2006

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

Reply via email to