Sign in Join

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

Mailing lists / fpc-pascal / 2010-10

Re: TInterfacedObject memory management

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Marco van de Voort
to Juha Manninen (gmail: Hi I have a program that creates a TInterfacedObject and nothing else. program project1;…

In our previous episode, Juha Manninen (gmail) said:

Show quoted text 7 lines
I have a program that creates a TInterfacedObject and nothing else. program project1; {$mode objfpc}{$H+} uses Classes; var
   io: IUnknown;
begin io := TInterfacedObject.Create; end.

Try the above program.

If I add "io.free" then there are no unfreed memory blocks. Now, I have understood interfaced objects are reference counted and freed automatically. Is this a bug or did I understand it wrong?

Yes you understood it wrong. The interface references are reference counted, the object references don't suddenly become ref-counted.

It is a common trap though.

Juha Manninen (gmail

Hi

I have a program that creates a TInterfacedObject and nothing else.

program project1;
{$mode objfpc}{$H+}
uses
  Classes;
var
  io: TInterfacedObject;
begin
  io := TInterfacedObject.Create;
end.

I compile it with -gh (heap trace) and get the following output:

[DBGTGT] Heap dump by heaptrc unit
[DBGTGT] 19 memory blocks allocated : 1191/1208
[DBGTGT] 18 memory blocks freed : 1167/1184
[DBGTGT] 1 unfreed memory blocks : 24
[DBGTGT] True heap size : 393216
[DBGTGT] True free heap : 393024
[DBGTGT] Should be : 393064
[DBGTGT] Call trace for block $00007FFFF7FAE6C0 size 24 [DBGTGT] $00000000004001F2 line 11 of project1.lpr [DBGTGT] $00000000004001B0

If I add "io.free" then there are no unfreed memory blocks. Now, I have understood interfaced objects are reference counted and freed automatically. Is this a bug or did I understand it wrong?

Regards,
Juha