[fpc-pascal] memory leaks with com interfaces?

Dean Zobec dezobec at tin.it
Sat Nov 13 14:09:46 CET 2004


I have some problems with com interfaces (refcounted):
in this simple program heaptrc reports a memory leak even if the destructor 
was correctly called:

{$mode objfpc}{$H+}
{$interfaces com}
program moneyleak;

type

  IMoney = interface
  ['{AAD734A1-6F35-D911-9C73-C6AC7996EDD0}']
  function Add(aMoney: IMoney): IMoney;
  end;

  TMoney = class(TInterfacedObject, IMoney)
  private
    FAmount: Int64;
    FCurrencyUnit: string;
  public
    function Add(aMoney: IMoney): IMoney;
    constructor Create(aAmount: int64; aCurrencyUnit: string);
    destructor Destroy; override;
  end;

  function TMoney.Add(aMoney: IMoney): IMoney;
  begin
    Result := nil;
  end;

  constructor TMoney.Create(aAmount: int64; aCurrencyUnit: string);
  begin
    Inherited Create;
    FAmount := aAmount;
    FCurrencyUnit := aCurrencyUnit;
  end;

  destructor TMoney.Destroy;
  begin
    FCurrencyUnit := '';
    writeln('Destroyed');
    inherited Destroy;
  end;

procedure TestLeak;
var
  a: IMoney;
begin
  a := TMoney.Create(12, 'EUR');
end;

begin
  TestLeak;
end.

The compilation and the results:

[dean at localhost demo]$ fpc -gh -gl moneyleak.pp
Free Pascal Compiler version 1.9.5 [2004/10/24] for i386
Copyright (c) 1993-2004 by Florian Klaempfl
Target OS: Linux for i386
Compiling moneyleak.pp
Linking moneyleak
50 Lines compiled, 0.1 sec
[dean at localhost demo]$ ./moneyleak
Destroyed
Heap dump by heaptrc unit
1 memory blocks allocated : 32/32
0 memory blocks freed     : 0/0
1 unfreed memory blocks : 32
True heap size : 557056 (16 used in System startup)
True free heap : 556944
Should be : 556952
Call trace for block $4000004C size 32
  $08052F26
  $08053922
  $0805C456  TMONEY__CREATE,  line 28 of moneyleak.pp
  $0805C60C  TESTLEAK,  line 45 of moneyleak.pp

What am I doing wrong?
Thank you in advance for any help,

Regards, 
  Dean Zobec





More information about the fpc-pascal mailing list