未使用的接口引用不会被破坏 [英] Unused interface reference is not destroyed
问题描述
ITest = interface
procedure Test;
结束
Tester = class(TInterfacedObject,ITest)
public
procedure Test;
结束
Base = class
public
procedure UseTestOrNot(test:ITest);虚拟;抽象;
结束
A = class(Base)
public
procedure UseTestOrNot(test:ITest);覆盖
结束
B = class(Base)
public
procedure UseTestOrNot(test:ITest);覆盖
结束
{A}
程序A.UseTestOrNot(test:ITest);
begin
test.Test();
结束
{B}
程序B.UseTestOrNot(test:ITest);
begin
WriteLn('No test here');
结束
// --------测试------------------------------- --------
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列表中每个B的测试程序泄漏:
在列表中的x
x.UseTestOrNot(Tester.Create);
//这是可以的
for x在列表中do
begin
t:= Tester.Create;
x.UseTestOrNot(t);
结束
list.Free;
结束。可以请你解释一下引用计数器出了什么问题吗?
你可以给出任何最佳做法/指南(例如永远不要在函数调用中创建一个接口实例[如果你不知道内部发生什么])。
我可以想到的这个例子的最好的解决方案是在类Base中编写一个模板方法,保存传递的测试实例并调用抽象的 DoUseTestOrNot
方法。
编辑
Delphi 2010
这是错误的不同表现这里。
我将其添加到QC报告中
这不再在Delphi XE更新1中复制。
- jeroen
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
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
这篇关于未使用的接口引用不会被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!