[fpc-pascal] Where and Why is there a memory leak?
mailinglists at geldenhuys.co.uk
Wed Sep 6 10:31:12 CEST 2017
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 ]============================
IHook = interface
THook = class(TInterfacedObject, IHook)
writeln(ClassName + ' did it');
TBaseClass = class(TInterfacedObject, IHook)
property Hook: IHook read FHook implements IHook;
destructor Destroy; override;
FHook := THook.Create; // FPC 2.6.4 reports a memory leak here
// nothing to do here
base := TBaseClass.Create;
base := nil; // just to see if it helped with the memory leak - it
==============================[ end ]==============================
When I run the program, the output is as follows:
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.
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
My public PGP key: http://tinyurl.com/graeme-pgp
More information about the fpc-pascal