TPacker.SplitNode

时间:2019-01-29 10:22:34

标签: delphi memory-leaks

我在网上找到了一个简单的二进制打包程序单元,我对其进行了更改以满足自己的需求。效果很好,但我正在泄漏内存。

TSprite

type
  TSprite=class
  public
    X:Integer;
    Y:Integer;
    Width:Integer;
    Height:Integer;
    Used:Boolean;
    Down:TSprite;
    Right:TSprite;
    constructor Create(X, Y,Width, Height:Integer);
end;

TPacker

type
  TPacker=class
  public
  Root:TSprite;
  constructor Create(W,H:Integer; Image : TImage);
  destructor Destroy; override;
  function Fit(var Blocks:array of TBlock):Boolean;
  function FindNode(root:TSprite; W, H:Integer):TSprite;
  function SplitNode(Node:Tsprite; W, H:Integer):TSprite;
end;

我这样使用它

var Packer:TPacker;

  Packer:=TPacker.Create(Width  , Height, Image);

  Packer.Fit(Blocks);

  Boundary(Packer.Root, Image);

  for I := Low(Blocks) to High(Blocks) do
    begin
      if Blocks[I].Fit <> nil then
        Draw(Blocks[I].Fit.X,Blocks[I].Fit.Y,Blocks[I].Width,Blocks[I].Height, Image);
    end;



  LeftRacks:=Report(blocks, packer.Root.Width, packer.Root.Height).Racks;
  FillPercent:=Report(blocks, packer.Root.Width, packer.Root.Height).FillPercent;

  Packer.Free;

TPacker的构造函数

constructor TPacker.Create(W: Integer; H: Integer; Image : TImage);
var temp : integer;
begin
  // 100x100 is our smalles unit . So to create a nice Packview we always
  // change W,H to neerest value which is dividable by 100.
  temp:=0;
  temp:=W mod 100;
  W:=W-temp;

  temp:=0;
  temp:= H mod 100;
  H:=H-temp;

  Image.Width:=W+1;
  Image.Height:=H+1;

  Self.Root := TSprite.Create(0,0,W,H);
  Self.Root.Used:=false;
  Self.Root.Down:=nil;
  Self.Root.Right:=nil;
end;

这是我第一次在析构函数中修复的内存泄漏

destructor TPacker.Destroy;
begin
  FreeAndNil(Root);

  inherited;
end;

当前泄漏发生在SplitNode中,因为它创建了新的节点,在这些节点中我失去了跟踪,而且我不知道如何正确释放它们。

function TPacker.SplitNode(Node: TSprite; W: Integer; H: Integer):TSprite;
begin
  Node.Used := true;
    Node.Down := TSprite.Create(Node.X , Node.Y + H , Node.Width , Node.Height - H);
    Node.Right := TSprite.Create(Node.X + W , Node.Y , Node.Width - W , H);
  Result := Node;
end;

SplitNode用于此Fit函数

function TPacker.Fit(var Blocks: array of TBlock):Boolean;
var
  I:Integer;
  Node:TSprite;
  temp:integer;
begin
   for I := Low(Blocks) to High(Blocks) do
    begin
      Node:=Self.FindNode(Self.Root, Blocks[I].Width, Blocks[I].Height);
      // we rotate it and try again just in case...
      if Assigned(Node) = false then
        begin
          temp:=Blocks[I].Width;
          Blocks[I].Width:=Blocks[I].Height;
          Blocks[I].Height:=temp;

          Node:=Self.FindNode(Self.Root, Blocks[I].Width, Blocks[I].Height);
        end;

      if Assigned(Node) then
        begin
          Blocks[I].Fit := Self.SplitNode(node, Blocks[I].Width, Blocks[I].Height);
        end;
    end;
end;

1 个答案:

答案 0 :(得分:6)

您明确创建并销毁了app.config(['$httpProvider', function($httpProvider) { $httpProvider.interceptors.push(function($log, $injector, $location) { return({ request: request, requestError: requestError, response: response, responseError: responseError }); function request(config) { // place your code logic inside here return config; } function requestError(rejection) { $log.debug(rejection); return $q.reject(rejection); } }); }]); 类中用作TSprite的{​​{1}}。但是Root在树结构中隐式创建了更多TPacker对象。没有自动机制可以清除这些其他对象。尽管引用了这些SplitNodeTSpriteTSprite),所以每个Down都引用了另外的Right,它们被拆分成可以清理的他们起来。如果每个人都进行清理,则将递归破坏整个树。

所以解决方案是为TSprite写一个析构函数:

TSprite