Recent

Author Topic: [solved] stack overflow error  (Read 562 times)

speter

  • Hero Member
  • *****
  • Posts: 527
[solved] stack overflow error
« on: May 03, 2026, 06:02:58 am »
I am getting an exception with the message "External: STACK OVERFLOW" in file uCommon.pas at line 238. begin"

When I click on OK, line 238 is indeed the "begin" of an initialisation procedure; which contains a number of calls to default(), which initialise various arrays in the program.

Recently, I've been reworking how I calculate the distance from a point to other positions on a game board / map.

Interestingly, the exception is occuring at the beginning of the procedure, not at one of the default() calls.

Code: Pascal  [Select][+][-]
  1. procedure GameInitialisation;
  2. var
  3.   a : integer;
  4. begin
  5.   debugform.AddMessage('begin GameInitialisation');
  6.   GameBoard := default(TGameBoard);
  7.   debugform.AddMessage(' a');
  8.   CityList := default(TCityList);
  9.   debugform.AddMessage(' b');
  10.   ...

The program is not displaying the "begin ..." message.

Any help / suggestions would be very welcome. I am thinking that the distance data is making the GameBoard variable too large...?

cheers
S.
« Last Edit: May 07, 2026, 01:06:34 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

ASerge

  • Hero Member
  • *****
  • Posts: 2492
Re: stack overflow error
« Reply #1 on: May 03, 2026, 08:08:52 am »
Code: Pascal  [Select][+][-]
  1. procedure GameInitialisation;
  2. var
  3.   a : integer;
  4. begin
  5.   debugform.AddMessage('begin GameInitialisation');
  6.   GameBoard := default(TGameBoard);...
When using Default(), FPC uses a temporary variable, which it initializes and assigns. Apparently the type of TGameBoard is very large.
FillChar(GameBoard, SizeOf(GameBoard), 0) may be suitable;

Thaddy

  • Hero Member
  • *****
  • Posts: 19118
  • Glad to be alive.
Re: stack overflow error
« Reply #2 on: May 03, 2026, 08:35:10 am »
Or gameboard is somehow on the stack (procedure local?) and a record type, not a pointer to record. If gameboard is a class that should not happen, because that is always a pointer type and allocated on the heap.
If it is a record (or old school object), a possible solution is to make gameboard a global variable, so it is on the heap or possibly locally use a pointer to gameboard and use New()/Dispose() to allocate on the heap and use pointer dereferences. Third option: increase the stack size with minstacksize and use the formula minstacksize := SizeOf(Gameboard) + Old Stack Size.
« Last Edit: May 03, 2026, 08:46:58 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

speter

  • Hero Member
  • *****
  • Posts: 527
Re: stack overflow error
« Reply #3 on: May 03, 2026, 08:51:54 am »
When using Default(), FPC uses a temporary variable, which it initializes and assigns. Apparently the type of TGameBoard is very large.
FillChar(GameBoard, SizeOf(GameBoard), 0) may be suitable;


Or gameboard is somehow on the stack (procedure local?) and a record type, not a pointer to record. If gameboard is a class that should not happen, because that is always a pointer type and allocated on the heap.
If it is a record, a possible solution is to make gameboard a global variable, so it is on the heap or possibly locally use a pointer to gameboard and use New()/Dispose() to allocate on the heap.

My thanks to you both.

Yes, the gameboard is an array of records. It is declared in a unit called udeclaractions.pas (:)) - which (I thought) would make it a global.

I think, I'll remove the pathing data from the gameboard and have it as a separate (array of records), possibly even as a 2d array of pointers (to the pathing info for the map).

cheers
S.

PS: ASerge's fillchar() suggestion removed the exception! :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 19118
  • Glad to be alive.
Re: stack overflow error
« Reply #4 on: May 03, 2026, 07:08:47 pm »
Yes, if you declared it in a unit, depending how, it is global so my remarks do not apply.
objects are fine constructs. You can even initialize them with constructors.

speter

  • Hero Member
  • *****
  • Posts: 527
Re: stack overflow error
« Reply #5 on: May 04, 2026, 12:52:44 am »
Yes, if you declared it in a unit, depending how, it is global so my remarks do not apply.
Mp worries. Thanks anyway.

cheers
S.
PS: My current thought (which I'll test today) is to replace the data with a function (since the game "geography" can change through the game).
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

jamie

  • Hero Member
  • *****
  • Posts: 7693
Re: stack overflow error
« Reply #6 on: May 04, 2026, 01:49:21 am »
Yes, if you declared it in a unit, depending how, it is global so my remarks do not apply.
Mp worries. Thanks anyway.

cheers
S.
PS: My current thought (which I'll test today) is to replace the data with a function (since the game "geography" can change through the game).

 If you put at the top of the source where your records are defined, this {$ModeSwitch AdvancedRecords} and insert in your records this.

Code: Pascal  [Select][+][-]
  1. TMyRecordTypeName = Record
  2. .......
  3. Class operator initialize(Var O:TMyRecordTypeName);
  4. End;
  5. ----
  6.  
  7. Implementation
  8.  
  9. class operator initialize(var O:TMyRecordTypeName)
  10. Begin
  11.  FillByte(O,SizeOF(O),0);
  12.  +++ any other inits you need here
  13. End;
  14.  
  15.  

That should have the compiler generate that code in the background, so you don't use Default.

Jamie;
The only true wisdom is knowing you know nothing

GannonRidge

  • Newbie
  • Posts: 2
Re: stack overflow error
« Reply #7 on: May 06, 2026, 04:41:44 pm »
All good now?
« Last Edit: May 06, 2026, 04:43:56 pm by GannonRidge »

speter

  • Hero Member
  • *****
  • Posts: 527
Re: stack overflow error
« Reply #8 on: May 07, 2026, 01:06:09 am »
All good now?
Yes!
As I hinted in my earlier post, I implemented the paths as 2 procedures, 1) CalcPaths(x,y) and 2) GetDirection(p1,p2). Where #1 calc's the paths around a given position and #2 calc's the where to move to head from p1 towards p2.

Despite the above, Jamie's operator initialize() could be very useful where I have Objects (at present I generally include an init() procedure).

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

GannonRidge

  • Newbie
  • Posts: 2
Re: [solved] stack overflow error
« Reply #9 on: May 07, 2026, 02:35:22 pm »
I am glad to hear that  :)

 

TinyPortal © 2005-2018