[fpc-devel] leak in destroy

mariano podesta antiriad at gmail.com
Tue Jun 21 23:18:38 CEST 2005


hi people,
i found a memory leak when calling Destroy explicitly.
the leak is in 1.9.8 and 2.0
has anyone seen this?

here is an example:

program mem3;

{$mode objfpc}{$H+}

type
  TTest = class (TObject, IUnknown)
  private
    FRefCount: Integer;
  public // IUnknown
    function QueryInterface(const IID: TGUID; out Obj): HResult;
virtual; stdcall;
    function _AddRef: Integer; virtual; stdcall;
    function _Release: Integer; virtual; stdcall;
  end;

function TTest.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
begin
  if GetInterface(IId, Obj) then
    Result := 0
  else
    Result := -1;
end;

function TTest._AddRef: Integer; stdcall;
begin
  Inc(FRefCount);
  Result := FRefCount;
end;

function TTest._Release: Integer; stdcall;
begin
  Dec(FRefCount);
  Result := FRefCount;
  if Result = 0 then
    Destroy;        // memory leak!!!
    //Self.Destroy; // works ok
end;

var
  o: IUnknown;
  s: THeapStatus;
  i: Integer;
begin
  for i := 0 to 9 do
  begin
    o := TTest.Create;
    o := nil;
    SysGetHeapStatus(s); // s := SysGetHeapStatus // for 2.0
    writeln('mem=', s.CurrHeapUsed); //TotalAllocated); // for 2.0
  end;

  readln;
end.


bye,
marianop




More information about the fpc-devel mailing list