Right. Like I say haven't done much Delphi lately and was going off the top of my head.
-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Heinz Sent: Friday, 21 July 2006 2:48 p.m. To: NZ Borland Developers Group - Delphi List Subject: RE: [DUG] Related try..finally question James wrote: > 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); Umm.. Free already tests against nil and bypasses Destroy if so. The root problem is calling Free when Y is non-nil as it has not been initialised yet and thus contains random stack contents. Local variables other than strings and variants (which are specially handled by the Delphi compiler) are not initialised to nil or 0 on method entry. So you need a 'Something := nil; X := nil; Y := nil;' statement block either just before the Try block (my preference) or immediately inside it. And remeber to nil the lot, including Something as if it fails inside the TSomething constructor, Something will still not be initialised yet. TTFN, Paul. _______________________________________________ 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
