当前位置:  开发笔记 > 编程语言 > 正文

工厂模式,内存泄漏

如何解决《工厂模式,内存泄漏》经验,为你挑选了1个好方法。

我正在阅读Hodges的书"Delphi中的更多编码",关于工厂模式的部分.试着学习东西.把我的代码分解成小单元.我使用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;
  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.Create;
end;

class destructor TGatewayFactory.Destroy;
begin
  FreeAndNil(FGatewayTypeAndFunctionList);
end;

initialization
  TGatewayFactory.AddGateway(
    gtSwedbank, 
    function: TBaseGateway
    begin
      Result := TSwedbankGateway.Create;
    end
  );

end.

David Heffer.. 9

这是一个编译器缺陷.在单元的初始化部分中定义匿名方法似乎导致匿名方法未完成,因此泄露.在这种情况下,我将解决问题,将代码从初始化部分移动到class constructor.

因此,initialization完全删除该部分,并将类构造函数更改为:

class constructor TGatewayFactory.Create;
begin
  FGatewayTypeAndFunctionList := TList.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中得到修复.



1> David Heffer..:

这是一个编译器缺陷.在单元的初始化部分中定义匿名方法似乎导致匿名方法未完成,因此泄露.在这种情况下,我将解决问题,将代码从初始化部分移动到class constructor.

因此,initialization完全删除该部分,并将类构造函数更改为:

class constructor TGatewayFactory.Create;
begin
  FGatewayTypeAndFunctionList := TList.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中得到修复.

推荐阅读
360691894_8a5c48
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有