[fpc-devel] Interfaced Object leak in inherited Object

alphax acmui_2004 at 163.com
Mon Jun 13 12:26:21 CEST 2005


----- Original Message ----- 
From: "Michael Van Canneyt" <michael at freepascal.org>
To: "FPC developers' list" <fpc-devel at lists.freepascal.org>
Sent: Monday, June 13, 2005 5:40 PM
Subject: Re: [fpc-devel] Interfaced Object leak in inherited Object


> 
> 
> On Mon, 13 Jun 2005, alphax wrote:
> 
> > It seems a bug in FPC 2.0.0.(Delphi 7.1 also has this bug)
> > 
> > Assumes there is an object(not class) declaration, the object has a IInterface field. 
> > If we declare another object derive from it, in the cleanup procedure of that decendant 
> > leak to clear the IInterface references.
> 
> This is normal behviour (although maybe not desirable behaviour). 
> You *must* call the destructor of your object, that is why it is there: 
> to clean up. 
> 
> Michael.
> 

That means I must declare a destructor for the object, and in the destructor I must call dtor explicitly and clear the Interface references Like this?

type
    ...
  TObj2 = object(TObj1)
    constructor Create;
    destructor Destroy;
    procedure Which; virtual;
  end;

destructor TObj2.Destroy;
    begin
        I := nil; //<* if comment out this line, the "I" field is still not cleanup *>
    end;

procedure Test2_1;
  var
    Obj: PObj2;
  begin
    WriteLn('Use Object Pointer');
    Obj := New(PObj2, Create);
    Obj^.I := TInterfacedObj.Create();
    Obj^.Which();
    Dispose(Obj, Destroy); //use this syntax
  end;

procedure Test2_2;
  var
    Obj: TObj2;
  begin
    WriteLn('Use Object directly');
    Obj.Create();
    Obj.I := TInterfacedObj.Create();
    Obj.Which();
    Obj.Destroy();
  end; 

I am modified the program and test again, I found if the TObj2 destructor is empty(comment out the I := nil; statement), the inteface reference still not cleanup, this is different to the TObj1, and defferent to record's finalization, is it a normal behaviour too?

alphax


More information about the fpc-devel mailing list