VyP*_*yPu 7 delphi memory-leaks unsafe weak aggregation
我想TParent通过使用聚合构建一个包含多个子对象的类.有些对象是独立的,有些也可能依赖于其他孩子.所有子对象都必须具有对父对象的引用.我也想尽可能使用接口.
为此我使用TInterfacedObject的TParent并TAggregatedObject为孩子.由于孩子和父母都知道彼此,我使用弱引用以避免循环依赖.事实上,这种行为已经在中定义TAggregatedObject.当我只使用独立的子对象(TIndependantChild)时,一切正常.
当子对象也依赖于其他孩子时会出现问题,请参阅构造函数TDependantChild.我将引用存储在fChild变量中的另一个子对象中,该变量用[weak]Delphi 10 Berlin中引入的attibute标记.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.
Run Code Online (Sandbox Code Playgroud)
我发现,使用[unsafe]而不是[weak]解决了这个问题,但根据delphi的帮助
它([不安全])应仅在非常罕见的情况下在系统单元外部使用.
因此,我不相信,我应该[unsafe]在这里使用,特别是当我不明白会发生什么时.
那么,在这种情况下内存泄漏的原因是什么以及如何克服它们?
使用外部 FastMM4 内存管理器时出现的泄漏和崩溃问题与以下有关用于跟踪弱引用的内部 HashMap 的最终确定的问题有关。
[REGRESSION XE2/10.1 Berlin] 无法使用第 3 方内存管理器
由于该问题,无法在所有受影响的版本(包括外部 FastMM4)中使用第 3 方内存管理器进行泄漏检测。
该问题已在 10.4 Sydney 中得到解决。
这就是为什么你对[weak]属性有问题而对 没有问题的原因[unsafe]。
就您的代码而言,您可以安全地[unsafe]在上述场景中使用。虽然文档中有关于使用[unsafe]属性的警告,但该警告实际上并没有解释为什么[unsafe]不应使用。
长话短说,[unsafe]当引用引用的对象实例的生命周期[unsafe]比引用本身的生命周期长时,可以使用属性。
换句话说,您必须确保[unsafe]在引用所指向的对象实例被释放后,您不会访问引用,仅此而已。
[unsafe]当它们指向的对象被销毁时,引用不会被清零,并且在对象消失后使用此类引用将导致访问冲突异常。
为了使您呈现的代码具有正确的功能,您只需将属性替换[weak]为 the即可。[unsafe]
TDependantChild = class(TAggregatedObject, IChild)
private
[unsafe] fChild: IChild;
Run Code Online (Sandbox Code Playgroud)