我想要构建一个类
我发现,使用 [unsafe] 而不是 [weak] 可以解决问题,但根据 Delphi 的帮助文档 help,
那么,在这种情况下,内存泄漏的原因是什么,如何克服它们?
TParent
,通过聚合包含多个子对象。其中一些对象是独立的,而有些对象也可以依赖于其他子对象。所有子对象都必须具有对父对象的引用。我还想在可能的情况下使用接口。
为此,我使用TInterfacedObject
作为TParent
,并使用TAggregatedObject
作为子对象。由于子对象和父对象都知道彼此,因此我使用弱引用以避免循环依赖。实际上,这种行为已经在TAggregatedObject
中定义。当我仅使用独立子对象(TIndependantChild
)时,一切都正常。
问题出现在子对象也依赖于其他子对象时,参见TDependantChild
的构造函数。我将对另一个子对象的引用存储在fChild变量中,该变量标记为[weak]
属性,在Delphi 10 Berlin中引入。FastMM4在关闭时报告内存泄漏:
访问冲突会导致System.TMonitor.Destroy
抛出错误,但只有在使用FastMM4且ReportMemoryLeaksOnShutDown为True时才会发生。
program Project1;
{$APPTYPE CONSOLE}
uses
FastMM4,
System.SysUtils;
type
IParent = interface
['{B11AF925-C62A-4998-855B-268937EF30FB}']
end;
IChild = interface
['{15C19A4E-3FF2-4639-8957-F28F0F44F8B4}']
end;
TIndependantChild = class(TAggregatedObject, IChild)
end;
TDependantChild = class(TAggregatedObject, IChild)
private
[weak] fChild: IChild;
public
constructor Create(const Controller: IInterface; const AChild: IChild); reintroduce;
end;
TParent = class(TInterfacedObject, IParent)
private
fIndependantChild: TIndependantChild;
fDependantChild: TDependantChild;
public
constructor Create;
destructor Destroy; override;
end;
{ TParent }
constructor TParent.Create;
begin
fIndependantChild := TIndependantChild.Create(Self);
fDependantChild := TDependantChild.Create(Self, fIndependantChild);
end;
destructor TParent.Destroy;
begin
fDependantChild.Free;
fIndependantChild.Free;
inherited;
end;
{ TDependantChild }
constructor TDependantChild.Create(const Controller: IInterface; const AChild: IChild);
begin
inherited Create(Controller);
fChild := AChild;
end;
var
Owner: IParent;
begin
ReportMemoryLeaksOnShutDown := True;
Owner := TParent.Create;
Owner := nil;
end.
我发现,使用 [unsafe] 而不是 [weak] 可以解决问题,但根据 Delphi 的帮助文档 help,
因此,我不确定是否应该在这里使用 [unsafe],特别是当我不理解发生了什么时。它([unsafe])应该只在 System 单元之外的非常罕见的情况下使用。
那么,在这种情况下,内存泄漏的原因是什么,如何克服它们?
[weak]
在10.1 Berlin中为接口添加,但是早期版本中已经存在了。我可以在XE2中按原样编译您的代码,但是[weak]
没有效果。当fChild
被分配时,由于聚合,TDependantChild
对TParent
有一个非弱引用,因此Owner
具有RefCount=1
。当销毁Owner
时,TInterfacedObject.BeforeDestruction()
会在RefCount<>0
时引发错误,导致Owner
及其子项泄漏。将fChild
更改为Pointer
可以解决这个问题。我怀疑在使用[unsafe]
时您的情况也类似。请通过调试器进行确认。 - Remy Lebeauprocedure TInterfacedObject.BeforeDestruction
上设置了调试器断点。没有[weak]
时,RefCount
确实为1,而有了[weak]
后,它变成了0。因此看起来,它在其他地方泄漏了。 - VyPu