我希望引用计数应该在接口实现中的外部聚合对象上工作。 如果我可以参考另外一个例子:Clarity in classes implementing multiple interfaces (alternative to delegation):德尔福接口实现
这里是行为的最小再现:
program SO16210993;
{$APPTYPE CONSOLE}
type
IFoo = interface
procedure Foo;
end;
TFooImpl = class(TInterfacedObject, IFoo)
procedure Foo;
end;
TContainer = class(TInterfacedObject, IFoo)
private
FFoo: IFoo;
public
constructor Create;
destructor Destroy; override;
property Foo: IFoo read FFoo implements IFoo;
end;
procedure TFooImpl.Foo;
begin
Writeln('TFooImpl.Foo called');
end;
constructor TContainer.Create;
begin
inherited;
FFoo := TFooImpl.Create;
end;
destructor TContainer.Destroy;
begin
Writeln('TContainer.Destroy called');//this line never runs
inherited;
end;
procedure Main;
var
Foo : IFoo;
begin
Foo := TContainer.Create;
Foo.Foo;
end;
begin
Main;
Readln;
end.
如果不是使用implements
,我实现了在TImplementor
类的接口,那么析构函数运行。
“我错过了什么吗?”我不知道。但我们当然是。你忘了包含代码!表明行为的完整程序是必需的。否则,我们必须猜测。 – 2013-04-25 09:24:34
你有一些额外的引用或引用循环。为TFirstSecond._AddRef和TFirstSecond._Release添加重写并在其中放置断点,获取完整的引用列表并查看哪些未被清除 – 2013-04-25 10:43:03
问题是,您的接口已委派。不知道为什么会导致这种行为。 – 2013-04-25 13:37:46