我在StackOverflow上测试了一段代码,并遇到了一个情况,隐式接口变量出现了。但我看不出这种情况的原因。我有一个迷你工厂,返回一个新创建对象的接口。如果我在过程块中调用该方法,则只得到1个引用计数。如果我在主程序块中调用它,则会得到2个引用计数。
我使用Delphi 10 Seattle进行了测试。是否有资源包含隐式接口创建的规则,以及通过工厂返回接口的模式是否可靠?
这里是第二个模块,我们可以看到隐式接口变量:
我使用Delphi 10 Seattle进行了测试。是否有资源包含隐式接口创建的规则,以及通过工厂返回接口的模式是否可靠?
program TestRefCount;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Vcl.Dialogs;
type
IMyInterface = interface(IInterface)
['{62EB2C46-9B8A-47CE-A881-DB96E6F6437D}']
procedure DoSomething;
function GetRefCount: Integer;
end;
TMyObject = class(TInterfacedObject, IMyInterface)
strict private
FMyValue: Integer;
public
procedure Init;
procedure DoSomething;
function GetRefCount: Integer;
end;
TMyFactory = class(TObject)
private
function CreateMyInt: IMyInterface;
end;
procedure TMyObject.DoSomething;
begin
MessageDlg(IntToStr(FMyValue), mtInformation, [mbok], 0);
end;
function TMyObject.GetRefCount: Integer;
begin
Result := FRefCount;
end;
procedure TMyObject.Init;
begin
FMyValue := 100;
end;
function TMyFactory.CreateMyInt: IMyInterface;
var
myObject: TMyObject;
begin
myObject := TMyObject.Create;
Assert(myObject.GetRefCount = 0);
myObject.Init;
Assert(myObject.GetRefCount = 0);
Result := myObject;
Assert(myObject.GetRefCount = 1);
Assert(Result.GetRefCount = 1);
end;
procedure WorkWithIntf;
var
myFactory: TMyFactory;
myInt: IMyInterface;
begin
myFactory := TMyFactory.Create;
try
myInt := myFactory.CreateMyInt;
Assert(myInt.GetRefCount = 1);
myInt.DoSomething;
Assert(myInt.GetRefCount = 1);
finally
myFactory.Free;
end;
end;
var
myFactory: TMyFactory;
myInt: IMyInterface;
begin
try
// This case doesn't have an implicit interface variable
WorkWithIntf;
// This case does have an implicit interface variable
myFactory := TMyFactory.Create;
try
myInt := myFactory.CreateMyInt;
Assert(myInt.GetRefCount = 1); // This fails because the refcount is 2
myInt.DoSomething;
Assert(myInt.GetRefCount = 1);
finally
myFactory.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
这是第一个块,其中没有隐式接口变量:
TestRefCount.dpr.67: myInt := myFactory.CreateMyInt;
005C6A5A 8D55F8 lea edx,[ebp-$08]
005C6A5D 8B45FC mov eax,[ebp-$04]
005C6A60 E83BFEFFFF call TMyFactory.CreateMyInt
TestRefCount.dpr.68: Assert(myInt.GetRefCount = 1);
005C6A65 8B45F8 mov eax,[ebp-$08]
这里是第二个模块,我们可以看到隐式接口变量:
TestRefCount.dpr.86: myInt := myFactory.CreateMyInt;
005CF513 8D55EC lea edx,[ebp-$14]
005CF516 A19CB75D00 mov eax,[$005db79c]
005CF51B E88073FFFF call TMyFactory.CreateMyInt
005CF520 8B55EC mov edx,[ebp-$14]
005CF523 B8A0B75D00 mov eax,$005db7a0
005CF528 E8C7E3E3FF call @IntfCopy
TestRefCount.dpr.87: Assert(myInt.GetRefCount = 1); // This fails because the refcount is 2
005CF52D A1A0B75D00 mov eax,[$005db7a0]