Sign in Join

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

Mailing lists / fpc-devel / 2005-06

leak in destroy

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

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

mariano podesta

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

Reply by email