【问题标题】:Delphi xe3 vcl to firemonkey array issueDelphi xe3 vcl到firemonkey数组问题
【发布时间】:2013-05-21 09:05:25
【问题描述】:

在 Delphi 中从 VCL 转换为 Firemonkey 时,我遇到以下问题: 在 VCL 中,我有以下数组: Tcarray=数组[1..$04000000]的T坐标; - 这很好用。

但是,在 Firemonkey 中声明相同的数组时,我会在 0x00 处得到一个关于堆栈溢出的异常。 到目前为止,我唯一的解决方案是将数组最小化为 [1..40000]。

这是为什么?有什么解决办法吗?

VCL代码如下

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;

会这样调用:

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; 

这在 VCL 中可以正常工作,但在 FM 中它会因以下错误而中断:“Project FMtest.exe 引发异常类 $C00000FD 并带有消息‘堆栈溢出在 0x00af69e7’。

【问题讨论】:

  • 你需要edit你的帖子添加实际代码(类型的完整声明,声明实际数组的代码,以及使用它的代码),以及确切的错误如果您希望我们提供帮助,您会收到有关两个版本的代码的异常消息。对问题的模糊描述可能是不够的,因为您所描述的不应该发生。
  • 根据需要进行了编辑。谢谢

标签: delphi delphi-xe3 firemonkey-fm2


【解决方案1】:

发生此堆栈溢出是因为您正在创建一个非常大的局部变量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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2012-09-29
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    相关资源
    最近更新 更多