SetLength / Move - 导致内存损坏

时间:2012-08-24 14:10:08

标签: delphi memory-management multidimensional-array delphi-2009

今天我偶然发现了导致我的阵列损坏的问题。这是一个可重现的测试用例:

unit Unit40;

interface

type
  TVertex = record
    X, Y: Double;
  end;

  TEdge = record
    V1, V2: TVertex;
  end;
  TEdges = array of TEdge;

type
  TBoundryInfo = array of TEdges;

procedure MemoryCorrupt;

implementation

procedure MemoryCorrupt;
var
  BoundryInfo: TBoundryInfo;
  i, PointIndex, BoundryLength: Integer;
begin
  BoundryLength := 57;
  PointIndex := 0;
  SetLength(BoundryInfo, BoundryLength);
  for i := 0 to BoundryLength - 1 do
  begin
    if i <> 17 then
    begin
      SetLength(BoundryInfo[i], 1);
      BoundryInfo[i][0].V1.X := 1;
      BoundryInfo[i][0].V2.X := 1;
      BoundryInfo[i][0].V1.Y := 1;
      BoundryInfo[i][0].V2.Y := 1;
    end else
    begin
      SetLength(BoundryInfo[i], 2);
      BoundryInfo[i][0].V1.X := 1;
      BoundryInfo[i][0].V2.X := 1;
      BoundryInfo[i][0].V1.Y := 1;
      BoundryInfo[i][0].V2.Y := 1;
      BoundryInfo[i][1].V1.X := 1;
      BoundryInfo[i][1].V2.X := 1;
      BoundryInfo[i][1].V1.Y := 1;
      BoundryInfo[i][1].V2.Y := 1;
    end;
  end;
  BoundryLength := 9;
  SetLength(BoundryInfo, BoundryLength);
  Move(BoundryInfo[PointIndex+1], BoundryInfo[PointIndex],
    ((BoundryLength - 1) - PointIndex) * SizeOf(BoundryInfo[PointIndex]));
  Dec(BoundryLength);
  Finalize(BoundryInfo[BoundryLength]);
  SetLength(BoundryInfo, BoundryLength); //After this, arrays contains garbage
  BoundryInfo[0][0].V1.X := 3;
end;

end.

我认为在SetLength之后的内存损坏只是Move使用不当的症状。 有人可以向我解释我做错了什么以及如何在这种情况下正确使用Move

在原始问题中,我在循环中从BoundryInfo中删除元素,这就是我调用Finalize(BoundryInfo[BoundryLength])

的原因

2 个答案:

答案 0 :(得分:10)

在您的代码中,

Move(BoundryInfo[PointIndex+1], BoundryInfo[PointIndex], 
  ((BoundryLength - 1) - PointIndex) * SizeOf(BoundryInfo[PointIndex]));

BoundryInfo[PointIndex+1]的指针复制到BoundryInfo[PointIndex]。这个指针是另一个动态数组,你必须要处理引用计数。

那是:

SetLength(BoundryInfo[PointIndex],0); // release memory
Move(BoundryInfo[PointIndex+1], BoundryInfo[PointIndex], 
  ((BoundryLength - 1) - PointIndex) * SizeOf(BoundryInfo[PointIndex]));
PPointerArray(BoundryInfo)^[BoundryLength-1] := nil; // avoid GPF

简而言之:

  • 完成将在move();
  • 期间覆盖的项目
  • 将nil写入最新项目,该项目由move()重复。

答案 1 :(得分:5)

通过使用Move和颠覆动态数组引用计数机制,您只需为自己设置陷阱。我强烈建议你坚持标准机制,让编译器担心细节。每次都会让他们正确。

for i := 0 to high(BoundaryInfo)-1 do
  BoundaryInfo[i] := BoundaryInfo[i+1];
SetLength(BoundaryInfo, Length(BoundaryInfo)-1);