Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Memory leak on TFPGInterfacedObjectList

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joao Morais

Hello list. Whenever I add interfaced objects to a TFPGInterfacedObjectList, those objects apparently add two references, while release only one when the list is cleaned or destroyed.

The following code shows:

==========>>>==========
$ /tmp/project1
just before vl.add(v1)..: 1
just after vl.add(v1)...: 3
after destroy the list..: 2
Heap dump by heaptrc unit
9 memory blocks allocated : 773/784
8 memory blocks freed : 757/768
1 unfreed memory blocks : 16
True heap size : 622592 (32 used in System startup) True free heap : 622464
Should be : 622480
Call trace for block $000F31E0 size 16
$000210DF _P$PROJECT1_finalize_implicit, of
/private/tmp//tmp/project1.lpr
$00021EF1 _P$PROJECT1_finalize_implicit, of
/private/tmp//tmp/project1.lpr
$00020E4F _P$PROJECT1_finalize_implicit, of
/private/tmp//tmp/project1.lpr
==========<<<==========

The code:

==========>>>==========
program Project1;
uses
   heaptrc, fgl, sysutils;
type
   i1 = interface(iinterface)
     function ref: integer;
   end;
   tl = specialize TFPGInterfacedObjectList<i1>;
   t1 = class(TInterfacedObject, i1)
   public
     function ref: integer;
   end;
var
   v1: i1;
   vl: tl;
function t1.ref: integer;
begin
   result := frefcount;
end;
begin
   v1 := t1.create;
   vl := tl.create;
   try
     writeln('just before vl.add(v1)..: ', v1.ref);
     vl.add(v1);
     writeln('just after vl.add(v1)...: ', v1.ref);
     vl.Clear;
   finally
     freeandnil(vl);
   end;
   writeln('after destroy the list..: ', v1.ref);
end.
==========<<<==========

Am I missing something?

Thanks!

Marco van de Voort
to Joao Morais

In our previous episode, Joao Morais said:

Hello list. Whenever I add interfaced objects to a TFPGInterfacedObjectList, those objects apparently add two references, while release only one when the list is cleaned or destroyed.

v1 reference will only be freed at the end, and also counts as one. Still, that doesn't explain the memleak, but to test it, try to nest it within a function instead of with globals.

Joao Morais
to Marco van de Voort
Marco van de Voort wrote 8 lines
In our previous episode, Joao Morais said: > Hello list. Whenever I add interfaced objects to a > TFPGInterfacedObjectList, those objects apparently add two references, > while release only one when the list is cleaned or destroyed. v1 reference will only be freed at the end, and also counts as one. Still, that doesn't explain the memleak, but to test it, try to nest it within a function instead of with globals.

The original leak was deep inside a project of mine. Anyway I nested the vars and the code inside a function (if I understood you correctly): same output, same problem. Btw I changed the generic list to TInterfaceList (test and my project) and the leak was gone.

Last but not least: 2.6.3 of a couple of weeks ago, Mac OS X.