0

When converting from VCL to Firemonkey in Delphi, I have the following issue: In VCL I have the following array: Tcarray=array[1..$04000000] of Tcoordinate; - This works fine.

However, declaring the same array in Firemonkey I get an exception about stack overflow at 0x00.. My only solution so far is to minimize the array to [1..40000].

Why is this? Any solution?

The VCL code is as follow

unit ptypes;

interface
uses windows,vcl.Graphics,Vcl.Imaging.jpeg,Vcl.Imaging.GIFImg,system.Types;

type
Tcoordinate=packed record
x,y:double;
end;

Tcarray=array[1..$04000000] of Tcoordinate;

Tpoly=packed record
   n:longint;
   c:Tcarray;
end;

It will be called like this:

procedure TForm12.Button2Click(Sender: TObject);
var
   poly:Tpoly;
begin
with poly do
begin
    c[1].x:=100; c[1].y:=100;
    c[2].x:=400; c[2].y:=100;
    c[3].x:=400; c[3].y:=400;
    c[4].x:=250; c[4].y:=550;
    c[5].x:=100; c[5].y:=400;
    n:=5;
end; 

This works fine in VCL but in FM it breaks with the following error: "Project FMtest.exe raised exception class $C00000FD with message 'stack overflow at 0x00af69e7'.

4

1 回答 1

2

发生此堆栈溢出是因为您正在创建一个非常大的局部变量poly(1.073.741.828 字节¡¡¡¡),而堆栈(存储局部变量的地方)的大小有限。

您可以避免此问题以这种方式重新声明您的类型

PCArray=^Tcarray;
Tcarray=array[1..$04000000] of Tcoordinate;


Tpoly=packed record
   n:longint;
   c:PCArray;
end;

像这样使用

var
   poly   : Tpoly; //now poly only uses 8 bytes of the stack space
   points : integer;
begin
  points:=5;
  GetMem(poly.c,sizeof(Tcoordinate)*points);
  try
    with poly do
    begin
      c[1].x:=100; c[1].y:=100;
      c[2].x:=400; c[2].y:=100;
      c[3].x:=400; c[3].y:=400;
      c[4].x:=250; c[4].y:=550;
      c[5].x:=100; c[5].y:=400;
      n:=points;
    end;
  finally
    FreeMem(poly.c);
  end;
end;
于 2013-05-22T17:38:42.657 回答