Delphi接口引用计数
今天我在测试东西时遇到了一个奇怪的情况 我有许多接口和对象。代码如下所示:Delphi接口引用计数,delphi,interface,delphi-xe4,reference-counting,Delphi,Interface,Delphi Xe4,Reference Counting,今天我在测试东西时遇到了一个奇怪的情况 我有许多接口和对象。代码如下所示: IInterfaceZ = interface(IInterface) ['{DA003999-ADA2-47ED-A1E0-2572A00B6D75}'] procedure DoSomething; end; IInterfaceY = interface(IInterface) ['{55BF8A92-FCE4-447D-B58B-26CD9B344EA7}'] procedure DoNothing
IInterfaceZ = interface(IInterface)
['{DA003999-ADA2-47ED-A1E0-2572A00B6D75}']
procedure DoSomething;
end;
IInterfaceY = interface(IInterface)
['{55BF8A92-FCE4-447D-B58B-26CD9B344EA7}']
procedure DoNothing;
end;
TObjectB = class(TInterfacedObject, IInterfaceZ)
procedure DoSomething;
end;
TObjectC = class(TInterfacedObject, IInterfaceY)
public
FTest: string;
procedure DoNothing;
end;
TObjectA = class(TInterfacedObject, IInterfaceZ, IInterfaceY)
private
FInterfaceB: IInterfaceZ;
FObjectC: TObjectC;
function GetBB: IInterfaceZ;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property BB: IInterfaceZ read GetBB implements IInterfaceZ;
property CC: TObjectC read FObjectC implements IInterfaceY;
end;
procedure TObjectB.DoSomething;
begin
Sleep(1000);
end;
procedure TObjectA.AfterConstruction;
begin
inherited;
FInterfaceB := TObjectB.Create;
FObjectC := TObjectC.Create;
FObjectC.FTest := 'Testing';
end;
procedure TObjectA.BeforeDestruction;
begin
FreeAndNil(FObjectC);
FInterfaceB := nil;
inherited;
end;
function TObjectA.GetBB: IInterfaceZ;
begin
Result := FInterfaceB;
end;
procedure TObjectC.DoNothing;
begin
ShowMessage(FTest);
end;
现在,如果我像这样访问各种实现,我会得到以下结果:
procedure TestInterfaces;
var
AA: TObjectA;
YY: IInterfaceY;
ZZ: IInterfaceZ;
NewYY: IInterfaceY;
begin
AA := TObjectA.Create;
// Make sure that the Supports doesn't kill the object.
// This line of code is necessary in XE2 but not in XE4
AA._AddRef;
// This will add one to the refcount for AA despite the fact
// that AA has delegated the implementation of IInterfaceY to
// to FObjectC.
Supports(AA, IInterfaceY, YY);
YY.DoNothing;
// This will add one to the refcount for FInterfaceB.
// This is also allowing a supports from a delegated interface
// to another delegated interface.
Supports(YY, IInterfaceZ, ZZ);
ZZ.DoSomething;
// This will fail because the underlying object is actually
// the object referenced by FInterfaceB.
Supports(ZZ, IInterfaceY, NewYY);
NewYY.DoNothing;
end;
第一个Supports调用使用实现中的变量,返回YY,它实际上是对TObjectA的引用。我的AA变量是引用计数的。因为底层引用计数对象是一个TObjectA,所以第二个supports使用supports调用中的接口,工作并返回一个接口。底层对象现在实际上是一个TObjectB。FInterfaceB后面的内部对象是被引用计数的对象。这一部分很有意义,因为GetBB实际上是FInterfaceB。正如这里所预期的,对Supports的最后一次调用为NewYY返回null,最后的调用失败
我的问题是,引用是否依赖于TObjectA和第一个支持设计调用?换句话说,当实现接口的属性返回一个对象而不是接口时,这是否意味着所有者对象将是执行引用计数的对象?我总是觉得实现也会导致内部委托对象被引用计数,而不是主对象
声明如下:
property BB: IInterfaceZ read GetBB implements IInterfaceZ;
使用上面的这个选项,FInterfaceB后面的内部对象就是引用计数的对象
property CC: TObjectC read FObjectC implements IInterfaceY;
使用上面的第二个选项,TObjectA是被引用计数的对象,而不是委托对象FObjectC
这是故意的吗
编辑
我刚刚在XE2中测试了这一点,结果有所不同。第二个Supports语句为ZZ返回nil。XE4中的调试器告诉我YY是指(TObjectA作为IInterfaceY)。在XE2中,它告诉我它是一个指针(如IInterfaceY)。此外,在XE2中,AA在第一个support语句中不进行ref计数,但内部FObjectC进行了reference计数
property CC: TObjectC read FObjectC implements IInterfaceY;
回答问题后的其他信息
对此有一个警告。可以链接接口版本,但不能链接对象版本。这意味着类似这样的东西会起作用:
TObjectBase = class(TInterfacedObject, IMyInterface)
…
end;
TObjectA = class(TInterfacedObject, IMyInterface)
FMyInterfaceBase: IMyInterface;
property MyDelegate: IMyInterface read GetMyInterface implements IMyInterface;
end;
function TObjectA.GetMyInterface: IMyInterface;
begin
result := FMyInterfaceBase;
end;
TObjectB = class(TInterfacedObject, IMyInterface)
FMyInterfaceA: IMyInterface;
function GetMyInterface2: IMyInterface;
property MyDelegate2: IMyInterface read GetMyInterface2 implements IMyInterface;
end;
function TObjectB.GetMyInterface2: IMyInterface;
begin
result := FMyInterfaceA;
end;
但是对象版本给出了一个编译器错误,表示TObjectB没有实现接口的方法
TObjectBase = class(TInterfacedObject, IMyInterface)
…
end;
TObjectA = class(TInterfacedObject, IMyInterface)
FMyObjectBase: TMyObjectBase;
property MyDelegate: TMyObjectBase read FMyObjectBase implements IMyInterface;
end;
TObjectB = class(TInterfacedObject, IMyInterface)
FMyObjectA: TObjectA;
property MyDelegate2: TObjectA read FMyObjectA implements IMyInterface;
end;
因此,如果您想开始链接委托,那么您需要坚持使用接口或以另一种方式解决它。您将对象指针和接口指针混合在一起,这总是导致灾难
TObjectA
不会增加其内部对象的引用计数,以确保它们在其整个生命周期内保持活动状态,TestInterfaces()
不会增加AA
的引用计数,以确保它在整个测试集中存活。对象指针不参与引用计数!您必须手动管理它,例如:
procedure TObjectA.AfterConstruction;
begin
inherited;
FObjectB := TObjectB.Create;
FObjectB._AddRef;
FObjectC := TObjectC.Create;
FObjectC._AddRef;
FObjectC.FTest := 'Testing';
end;
procedure TObjectA.BeforeDestruction;
begin
FObjectC._Release;
FObjectB._Release;
inherited;
end;
不用说,手动引用计数破坏了接口的使用
在处理接口时,您需要:
tl;dr这一切都是出于设计——只是设计在XE2和XE3之间发生了变化 XE3及更高版本 委托给接口类型属性和委托给类类型属性之间有很大的区别。事实上,对于这两个委托变体,使用不同的部分明确地指出了这一差异 从您的角度来看,区别如下:
property BB: IInterfaceZ read GetBB implements IInterfaceZ;
- 当
通过委托给类类型属性TObjectA
来实现CC
时,实现对象是iinterface
的实例TObjectA
- 当
通过委托接口类型属性TObjectA
来实现BB
时,实现对象是实现IInterfaceZ
的对象FInterfaceB
IInterface
,也不需要有\u AddRef
和\u Release
方法
要看到这一点,请修改代码对TObjectC
的定义如下:
TObjectC = class
public
procedure DoNothing;
end;
您将看到此代码的编译、运行和行为与您的版本完全相同
事实上,理想情况下,这就是将接口委托给的类声明为类类型属性的方式。这样做可以避免混合接口和类类型变量的生存期问题
那么,让我们看看您对支持的三个调用:
Supports(AA, IInterfaceY, YY);
这里的实现对象是AA
,因此AA
的参考计数增加
Supports(YY, IInterfaceZ, ZZ);
Supports(ZZ, IInterfaceY, NewYY);
这里的实现对象是TObjectB
的实例,因此它的引用计数是递增的
Supports(YY, IInterfaceZ, ZZ);
Supports(ZZ, IInterfaceY, NewYY);
这里,ZZ
是由TObjectB
的实例实现的接口,它不实现IInterfaceY
。因此支持返回False
和NewYY
为nil
XE2及更早版本
XE2和XE3之间的设计更改与MobileArm编译器的引入相一致,并且有许多低级更改来支持ARC。显然,其中一些更改也适用于桌面编译器
我可以发现的行为差异涉及接口实现对类类型属性的委托。特别是在上课的时候