我正在阅读Hodges的书“Delphi中的更多编码”,关于工厂模式的部分.
试着学习东西.把我的代码分解成小单元.
我使用ReportMemoryLeaksOnShutDown:= True;并且停止代码会导致内存泄漏.为什么会发生,我该如何解决?
试着学习东西.把我的代码分解成小单元.
我使用ReportMemoryLeaksOnShutDown:= True;并且停止代码会导致内存泄漏.为什么会发生,我该如何解决?
unit Unit2; interface uses Generics.Collections,System.SysUtils; type TGatewayTpe = (gtSwedbank,gtDNB); type TBaseGateway = class end; type TSwedbankGateway = class(TBaseGateway) end; type TGatewayFunction = reference to function: TBaseGateway; type TGatewayTypeAndFunction = record GatewayType: TGatewayTpe; GatewayFunction: TGatewayFunction; end; type TGatewayFactory = class strict private class var FGatewayTypeAndFunctionList: TList<TGatewayTypeAndFunction>; public class constructor Create; class destructor Destroy; class procedure AddGateway(const AGatewayType: TGatewayTpe; const AGatewayFunction: TGatewayFunction); end; implementation class procedure TGatewayFactory.AddGateway(const AGatewayType: TGatewayTpe; const AGatewayFunction: TGatewayFunction); var _GatewayTypeAndFunction: TGatewayTypeAndFunction; begin _GatewayTypeAndFunction.GatewayType := AGatewayType; _GatewayTypeAndFunction.GatewayFunction := AGatewayFunction; FGatewayTypeAndFunctionList.Add(_GatewayTypeAndFunction); end; class constructor TGatewayFactory.Create; begin FGatewayTypeAndFunctionList := TList<TGatewayTypeAndFunction>.Create; end; class destructor TGatewayFactory.Destroy; begin FreeAndNil(FGatewayTypeAndFunctionList); end; initialization TGatewayFactory.AddGateway( gtSwedbank,function: TBaseGateway begin Result := TSwedbankGateway.Create; end ); end.
解决方法
这是一个编译器缺陷.在单元的初始化部分中定义匿名方法似乎导致匿名方法未完成,因此泄露.在这种情况下,我将通过将代码从初始化部分移动到类构造函数来解决此问题.
class constructor TGatewayFactory.Create; begin FGatewayTypeAndFunctionList := TList<TGatewayTypeAndFunction>.Create; AddGateway( gtSwedbank,function: TBaseGateway begin Result := TSwedbankGateway.Create; end ); end;
这是我可以编写的最简单的复制品:
unit Unit1; interface implementation type TProc = reference to procedure; var Foo: TProc; initialization ReportMemoryLeaksOnShutdown := True; Foo := procedure begin end; end.
将此单元包含在项目中时,将报告匿名方法泄露.
但是这个变种没有报告泄漏:
unit Unit1; interface implementation type TProc = reference to procedure; var Foo: TProc; procedure DoInit; begin Foo := procedure begin end; end; initialization ReportMemoryLeaksOnShutdown := True; DoInit; end.
该缺陷在XE8中得到修复.
原文地址:https://www.jb51.cc/delphi/102259.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。