Search code examples
delphidelphi-xe3firemonkey-fm2

Delphi xe3 vcl to firemonkey array issue


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'.


Solution

  • This stack overflow occurs because you are creating a very large local variable poly (of 1.073.741.828 bytes ¡¡¡¡) and the stack (the place where the local variables are stored) has a limited size.

    You can avoid this issue redeclarating your types in this way

    PCArray=^Tcarray;
    Tcarray=array[1..$04000000] of Tcoordinate;
    
    
    Tpoly=packed record
       n:longint;
       c:PCArray;
    end;
    

    And use like so

    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;