TPacker.SplitNode中的内存泄漏

use*_*012 1 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;
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

我这样用它

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;
Run Code Online (Sandbox Code Playgroud)

构造函数 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;
Run Code Online (Sandbox Code Playgroud)

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

destructor TPacker.Destroy;
begin
  FreeAndNil(Root);

  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

当前的泄漏发生在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;
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

nil*_*nil 6

您明确创建和销毁TSprite使用RootTPacker类.但是在树结构中隐式地SplitNode创建了更多的TSprite对象.没有自动化可以清理这些额外的对象.TSprite虽然(DownRight)引用了这些s ,所以每个TSprite都引用了TSprite它被拆分的附加部分并且可以清理它们.如果每个人都清理干净,整个树将被递归销毁.

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

destructor TSprite.Destroy;
begin
  Down.Free;
  Right.Free;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)