> >> It happens when you try to free an object which either you didn't
> >> create, or you've already set free.
> >
> > Only when the pointer to the object is containin nil. This
> is not the
> > case if you free an object and also not the case if the variable
> > pointing to that object is on the stack and it is not created.
> >
>
> classes declared with the keyword class are never stored on the stack.
>

I haven't followed your discussion, just mentioning here that

X.Free

Should never fail since it's a class method and its implementation checks if
X is nil. If it's not it calls the destructor of "self" (which is the hidden
param each method gets passed to and will be X in this case)

Procedure TObject.Free;
begin
 if self<>nil then
  self.Destroy;
end;

It doesn't do self:=nil, think it would corrupt the runtime if it did

To clear the pointer there's the "FreeAndNil" function, that is:

Function FreeAndNil(var x);
begin
 if x<>nil then
  begin
  TObject(x).Free;
  x:=nil;
  end;
end;

Never call FreeAndNil on an interface reference (or anything else than a
TObject reference) in Delphi, it will crash and burn. I think the "bug" in
Delphi's implementation is that it should have used a typed "x" parameter at
FreeAndNil, not an untyped pointer. Not sure if this has been fixed after
Delphi7 original version (maybe at some update or at higher Delphi versions)

Function FreeAndNil(var x:TObject);
begin
 if x<>nil then
  begin
  TObject(x).Free;
  x:=nil;
  end;
end;

----------------
George Birbilis ([EMAIL PROTECTED])
Microsoft MVP J# for 2004-2006
Borland "Spirit of Delphi"
* QuickTime, QTVR, ActiveX, VCL, .NET
http://www.kagi.com/birbilis
* Robotics
http://www.mech.upatras.gr/~Robotics
http://www.mech.upatras.gr/~robgroup




  _____  

avast! Antivirus <http://www.avast.com> : Outbound message clean. 


Virus Database (VPS): 0623-0, 05/06/2006
Tested on: 5/6/2006 3:15:17 ??
avast! - copyright (c) 1988-2006 ALWIL Software.



_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to