Hello,
Instead of .Free in your FreeVars routine, you should use FreeAndNil( theobject).
The correct lifecycle for class objects is :
var
anObject : TMyObject;
begin
anObject := nil; //not needed for class attributes. Required for local function or global unit variables.
anObject := TMyObject.Create( params) //if an exception is raised here, the anObject reference will still be nill.
try
//... do some code on anObject...
finally
FreeAndNil( anObject); //after this, the memory referenced by anObject is freed, and the reference anObject is set to nil.
end;
end;
Hope this helps,
Thierry
----- Mail Original -----
De: "Frank Church"
À: "FPC-Pascal users discussions"
Envoyé: Samedi 22 Octobre 2011 09h08:17 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: Re: [fpc-pascal] How are Assigned, Free, Nil and Destroy related?
On 22 October 2011 07:20, Felipe Monteiro de Carvalho < > wrote:
I understand Assigned as being the same as nil, so Assigned(Object) = Object nil
I vaguely remember that it could be safer in some corner case, but I don't remember ever meting that.
Free is how you release the memory allocated for a object. Free calls Destroy. Never call Destroy manually. When you implement the destructor you always implement Destroy, never change Free.
Nil is not a routine, it is a value, it means that the object is empty, it does not exist / is not allocated. Nil in existing implementations that I know is represented by the value zero.
The typical life-cycle of a object is:
MyObject := TMyObject.Create;
try
MyObject.DoSomething();
finally
MyObject.Free;
end;
To implement this object you should implement Create, DoSomething and Destroy.
Felipe Monteiro de Carvalho