7

i had another bug in my app caused by careless usage of Delphi interfaces. When i pass an interface to a procedure which ignores that argument, the instance is never freed. See the following simple example:

ITest = interface
    procedure Test;
end;

Tester = class(TInterfacedObject, ITest)
public
    procedure Test;
end;

Base = class
public
    procedure UseTestOrNot(test : ITest); virtual; abstract;
end;

A = class(Base)
public
    procedure UseTestOrNot(test : ITest); override;
end;

B = class(Base)
public
    procedure UseTestOrNot(test : ITest); override;
end;

{ A }

procedure A.UseTestOrNot(test: ITest);
begin
    test.Test();
end;

{ B }

procedure B.UseTestOrNot(test: ITest);
begin
    WriteLn('No test here');
end;

// -------- Test ---------------------------------------
var
    list : TObjectList<Base>;
    x : Base;
    t : ITest;
begin
    ReportMemoryLeaksOnShutdown := true;

    list := TObjectList<Base>.Create;
    list.Add(A.Create);
    list.Add(B.Create);

    // 1 x Tester leak for each B in list:
    for x in list do
        x.UseTestOrNot(Tester.Create);

    // this is ok
    for x in list do
    begin
        t := Tester.Create;
        x.UseTestOrNot(t);
    end;

    list.Free;
end.

Can you please explain what goes wrong with the reference counter? Can you give any best practice/ guideline (like "Never create an interfaced instance inside a function call [if you don't know what happens inside]).

The best solution i can think of for this example is to write a template method in class Base that saves the passed test instance and calls an abstract DoUseTestOrNot method.

EDIT Delphi 2010

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
hansmaad
  • 18,417
  • 9
  • 53
  • 94

2 Answers2

8

Add a guid to you ITest declaration

ITest = interface
['{DB6637F9-FAD3-4765-9EC1-0A374AAC7469}']
    procedure Test;
end;

Change the loop to this

for x in list do
    x.UseTestOrNot(Tester.Create as ITest);

The GUID is neccesary to be able to use as

Test.Create as ITest makes the compiler to add the release where the created object goes out of scope.

Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
  • Thank your for this workaround. Is there any menu entry or button to insert the GUID? I always forget the shortcut, as i never use GUIDs (i always try to avoid as and is etc). – hansmaad Jan 14 '11 at 06:57
8

It is a different manifestation of the bugs here.
I will add this to the QC report.

This does not reproduce in Delphi XE update 1 any more.

--jeroen

Community
  • 1
  • 1
Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • For regression: can you mail me a fully functional console program (.dpr) showing the memory leak in Delphi 2010 - preferably with FastMM? Anything at pluimers dot com works, just start with my first name. That way I can rule out any typos I made from making your above code compile. – Jeroen Wiert Pluimers Jan 14 '11 at 11:00