Conor wrote: > 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.
Actually, thinking this all through, for maximum paranoia and correctness, I think Conor is actually dead right that this is the best method. Adding Dennis's ' multiple try Free except inside single finally' model adds just as many exception frames to ensure all Frees are done. Adding a SafeFree method would reduce the metadata overhead i.e. procedure SafeFree(AObject: TObject) begin try AObject.Free; (or FreeAndNil(AObject) is you prefer) except end; end; But Conor's standard model gives this effect anyway i.e. any failing Free cascades up naturally to the next outer finally. Dennis's model also has the side effect of swallowing any destructor exceptions which hides information and potentially hinders debuggability in the field. YMMV. So the only problem is all the typing which is why Anders went and added 'using' to C# (and VB.NET?). So there is no excuse (in C# anyway :-) to avoid the correct pattern. Smart guy that Anders :-) Hopefully Delphi for Win32 will acquire 'using' from .NET at some point. Does Delphi.NET support 'using'? TTFN, Paul. _______________________________________________ Delphi mailing list [email protected] http://ns3.123.co.nz/mailman/listinfo/delphi
