Hi,

Playing with this small sample application to answer another question in this mailing list, I noticed the sample application has a memory leak. For the life of me I can't see why or how to resolve it.

I tested with FPC 2.6.4, 3.0.2 and 3.0.4-rc1 under 64-bit FreeBSD.

=======================[ project1.pas ]============================
program project1;

{$mode objfpc}{$H+}
{$interfaces COM}

type
  IHook = interface
    ['{4BCAEDD8-92D8-11E7-88D3-C86000E37EB0}']
    procedure DoIt;
  end;

type
  THook = class(TInterfacedObject, IHook)
  private
    procedure DoIt;
  end;

  procedure THook.DoIt;
  begin
    writeln(ClassName + ' did it');
  end;

type
  TBaseClass = class(TInterfacedObject, IHook)
  private
    FHook: IHook;
    property Hook: IHook read FHook implements IHook;
  public
    constructor Create;
    destructor Destroy; override;
  end;

  constructor TBaseClass.Create;
  begin
    FHook := THook.Create;  // FPC 2.6.4 reports a memory leak here
  end;

  destructor TBaseClass.Destroy;
  begin
    // nothing to do here
  end;


var
  base: IHook;

begin
  base := TBaseClass.Create;
  base.DoIt;
base := nil; // just to see if it helped with the memory leak - it doesn't

end.
==============================[ end ]==============================


When I run the program, the output is as follows:

[t1]$ ./project1
THook did it
Heap dump by heaptrc unit
4 memory blocks allocated : 115/120
2 memory blocks freed     : 51/56
2 unfreed memory blocks : 64
True heap size : 1114112 (32 used in System startup)
True free heap : 1113696
Should be : 1113760
Call trace for block $000000080072F180 size 32
  $0000000000400379 line 35 of project1.lpr
Call trace for block $000000080072F0C0 size 32



Personally I always use CORBA style interfaces, never reference counted COM style interfaces. So my programs normally don't have this issue, and I use interfaces a lot.




Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to