[DUG] Related try..finally question
Conor Boyd
Conor.Boyd at trimble.co.nz
Fri Jul 21 15:06:19 NZST 2006
Yeah, I know all that.
To be honest, I don't find much use for FreeAndNil.
As you say, it's pointless inside a method.
For variables with greater scope than just a method, most of the time
I'm creating objects in a constructor and free-ing them in the
destructor, in which case FreeAndNil is again irrelevant.
C.
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz]
On Behalf Of Guy.Brown at sungard.com
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.
[snip]
More information about the Delphi
mailing list