Delphi:使用[weak]属性的对象聚合和内存泄漏
我想通过使用聚合构建一个包含多个子对象的类TParent.有些对象是独立的,有些也可能依赖于其他孩子.所有子对象都必须具有对父对象的引用.我也想尽可能使用接口.
为此,我使用TentterfacedObject作为子宫的TParent和TAggregatedObject.由于孩子和父母都知道彼此,我使用弱引用以避免循环依赖.实际上,这种行为已在TAggregatedObject中定义.当我只使用独立的子对象(TIndependantChild)时,一切正常. 当子对象也依赖于其他子对象时会出现问题,请参阅TDependantChild的构造函数.我将引用存储在fChild变量中的另一个子对象中,该变量标记为Delphi 10 Berlin中引入的[weak]属性. 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
因此,我不相信,我应该在这里使用[不安全],特别是当我不明白会发生什么时. 那么,在这种情况下内存泄漏的原因是什么以及如何克服它们? 解决方法
使用外部FastMM4内存管理器时泄漏和崩溃的问题与以下有关用于跟踪弱引用的内部HashMap的最终确定有关.
[REGRESSION XE2/10.1 Berlin] Unable to use 3rd party memory managers 由于该问题,在Delphi 10.1和更新版本中使用第三方内存管理器进行泄漏检测是不可能的,包括外部FastMM4. 这就是为什么你有[弱]属性的问题,你没有[不安全]的问题. 就您的代码而言,您可以在上面的场景中安全地使用[unsafe].虽然文档中有关于使用[unsafe]属性的警告,但该警告实际上并不能解释为什么不应该使用[unsafe]. 简而言之,当[unsafe]引用引用的对象实例的生命周期长于引用本身的生命周期时,可以使用[unsafe]属性. 换句话说,您必须确保在它指向的对象实例已被释放后才能访问[unsafe]引用,这就是全部. 当它们指向的对象被销毁时,[unsafe]引用不会被清零,并且在对象消失后使用这样的引用将导致访问冲突异常. 用[unsafe]替换[weak]属性就是为了获得正确的功能代码所需要做的全部工作. TDependantChild = class(TAggregatedObject,IChild) private [unsafe] fChild: IChild; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |