我发现我的代码泄漏了TInterfacedObject
实例的内存,我将其作为接口引用保留。虽然我在使用后将参考变量重置为nil
,但它仍然存活。
泄漏的对象属于TMasterObject
类,它实现了两个接口IInterfaceX
和IInterfaceY
。对象的引用保存在IInterfaceY
类型的变量中。
TMasterObject
IInterfaceX
的实施仅仅是巧合。因为它有两个TSomeObject
实例,需要引用此接口,所以我也在TMasterObject
中实现了它。
program InterfaceDependencies;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
IInterfaceX = interface end;
IInterfaceY = interface end;
TSomeObject = class(TObject)
FReference: IInterfaceX;
constructor Create(AReferenceX: IInterfaceX);
end;
TMasterObject = class(TInterfacedObject, IInterfaceX, IInterfaceY)
FObjectA: TSomeObject;
FObjectB: TSomeObject;
constructor Create;
destructor Destroy; override;
end;
{ TSomeObject }
constructor TSomeObject.Create(AReferenceX: IInterfaceX);
begin
FReference := AReferenceX;
end;
{ TMasterObject }
constructor TMasterObject.Create;
begin
FObjectA := TSomeObject.Create(Self); // increments "FRefCount" by 1
FObjectB := TSomeObject.Create(Self); // increments "FRefCount" by 1
end;
destructor TMasterObject.Destroy;
begin
FObjectA.Free;
FObjectB.Free;
inherited;
end;
var
LMasterObject: IInterfaceY;
begin
try
LMasterObject := TMasterObject.Create;
// 'TMasterObject(LMasterObject).FRefCount' is now "3" because of 'FObjectA.FReference' and 'FObjectB.FReference'
LMasterObject := nil; // decrements 'TMasterObject(LMasterObject).FRefCount' by 1
// 'LMasterObject' is not destroyed because 'TMasterObject(LMasterObject).FRefCount' is still "2"
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
可以使用哪些策略