[fpc-pascal] Where and Why is there a memory leak?
Graeme Geldenhuys
mailinglists at geldenhuys.co.uk
Wed Sep 6 11:24:51 CEST 2017
On 2017-09-06 09:37, Michael Van Canneyt wrote:
>> 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;
> You must free FHook here, because you are keeping a reference to the object,
> not the interface.
I changed the destructor to the code shown below. Just so you know, I
tried this before I posted the message, and it didn't make any
difference. FHook is a IHook interface reference, not a object
reference. I can't change it either, otherwise FPC gives me a compiler
error on the property ... implements...; line.
destructor TBaseClass.Destroy;
begin
inherited;
FHook := nil;
end;
And the program output gives:
[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
Still 2 memory leaks. :-(
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
More information about the fpc-pascal
mailing list