stack size?

11 views
Skip to first unread message

james cardona

unread,
Feb 1, 2013, 9:58:41 AM2/1/13
to fre...@googlegroups.com
I am not sure exactly when initial variables are loaded onto the stack so perhaps someone who understands more of the core can help me find a bug.  I am writing another video mode that has a lot of small objects - so then there are a lot of variables to track these objects.  Initially I had less objects and then correspondingly less variables and everything ran fine.  Now I increased my array size and the game crashes at the ENTRANCE to that video mode.  I suspect that I am exceeding the maximum number of variables? or stack size?

For example, I have a file called vm_chooser that will decide which video mode to run and if it calls start_shooting_gallery, the game crashes immediately - all page declarations are correct and these two files are on the same page anyway.

Brian Dominy

unread,
Feb 1, 2013, 10:19:12 AM2/1/13
to fre...@googlegroups.com
Not sure if this is the problem, but it has bitten me many times.

There is a limit to the amount of stack space you can have *when the process is sleeping*.  As long as you don't call task_sleep() or any function that might, you're unlikely to be hitting a limitation.  Why?  When a process is running, it has a fairly spacious stack - 300-400 bytes.  I suppose you might hit that limit if you were deeply nesting functions, but I've never seen it.

However, when a task sleeps, its stack has to be saved away into a separate data structure, which has much less space - 40 bytes.  It's very easy to hit this limit if you're not careful.
This restriction is due to the limited RAM size to start with and the number of possible tasks that have to run in parallel - there just isn't enough RAM to make this bigger.

If you find yourself using a lot of small variables, and the function doesn't need to be reentrant (i.e. called from multiple tasks simultaneously), you're better off taking these off the stack and making them global.  Then they don't get swapped in/out on every task switch.  Thinking about a video mode, if your variables are tracking the states of all the screen objects, these really shouldn't be on the stack.

It's not clear if it's a true crash, or if the system is detecting the error and calling fatal().  There is code to try to detect this problem and shutdown cleanly before things get weird.

If you're not calling sleep somewhere in this routine, though, then it must be something else...

Brian

james cardona

unread,
Feb 1, 2013, 10:29:25 AM2/1/13
to fre...@googlegroups.com, br...@oddchange.com
all the vars are global and at the top of that particular video mode file.  But for example - I have a struct with 9 U8's and there is an array of 35 of those so 9x35 = 315 bytes.  I also have another array of struct of 3x10 for another 30 bytes - I think I have over 400 bytes in globals counting everything.  The functions have sleeps but they do not have any vars in the functions to speak of.

So I guess the globals for a particular file are not initialized until a function in that file is called?

Brian Dominy

unread,
Feb 1, 2013, 10:34:02 AM2/1/13
to fre...@googlegroups.com
If you're already using global variables then none of the previous message applies...

The limitation on size is strictly enforced by the build system; you would get an error compiling if you went over the limit.

As far as initialization, variables are _never_ initialized automatically.  Are you declaring globals like this?

    U8 my_var = 0;

If so, don't do that :)  Remove the initializer and assign it explicitly when you want it initialized.  For a game mode, you would initialize them when the mode starts.  Or maybe some of the state really needs to be set at powerup - then put those in an "init" event handler.

Brian

james cardona

unread,
Feb 4, 2013, 7:52:14 AM2/4/13
to fre...@googlegroups.com, br...@oddchange.com
Okay, so I did some experimentation over the weekend and the problem certainly is too many globals.  Specifically too large an array.  I tweaked the array size down and the crashes went away.  Tweaked back up and crash came back.  I am guessing that the arrays are not allocated until the code enters that file space since the game runs fine until a my video mode start function is called.

In any case, something for other folks to watch for if you are doing any work like this.  I am rewriting my code such that I won't need as large an array and I think I should be good.

Brian Dominy

unread,
Feb 4, 2013, 7:54:36 AM2/4/13
to fre...@googlegroups.com
Can you post the failing code somewhere so that I can look at it?  I'm still not sure how too many globals is the problem.  How are your initializing it?
- Brian

--
 
---
You received this message because you are subscribed to the Google Groups "FreeWPC" group.
To unsubscribe from this group and stop receiving emails from it, send an email to freewpc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
- Brian

james cardona

unread,
Feb 4, 2013, 9:00:51 AM2/4/13
to fre...@googlegroups.com, br...@oddchange.com
here is the variables section unaltered:

////////////////////////////////
//game play variables
////////////////////////////////
U8                 sgSpeed;
U8                sgCounter;
__local__ U8     sgLevel;         //local means to store data per player
__boolean         game_not_over;
score_t         sgScore;
U8                numTOadvance;
U8                sound_timer;
__boolean         sound_toggle;

////////////////////////////////
//gun and bullet variables
////////////////////////////////
U8             gun_xpos;
U8             numBulletsLeft;
U8             numBullets;
U8             Bullet_Index; //pointer to next bullet in chamber
__boolean     isRshot; //a bullet was fired this frame
__boolean     isLshot;
U8                 bullet_xpos[20];
U8                 bullet_ypos[20];
U8                 bullet_state[20];


////////////////////////////////
//target variables
////////////////////////////////
U8                 totalNumTargets;
U8                 targetIndex;                    //pointer to next target in chute
U8                target_shot_frame_counter;
U8                next_target_delay;                //delay time to wait before sending out next target
U8                 num_targets_out_of_play;         //out of play = shot plus fell off screen
U8                 num_targets_shot;

struct target_struct{
    U8                 xpos;
    U8                 ypos;
    U8                level;
    U8                width;
    __boolean        swap;
    U8                 swap_counter;
    U8                swap_speed;
    U8                target_state; 
    U8                target_type;
} target[35];





This is 355 U8's.  If I decrease amount of targets then crash goes away.  I haven't narrowed down to the exact # limit but 355 is too many.  I don't understand how this could be the problem either.  Vars are init'ed in a later function but the crash happens before that function is called.  Crash occurs at call of below function start_shooting_gallery - First line of this function is never executed on crash.


void start_shooting_gallery (void) { //this is the first function called
    ball_search_monitor_stop ();
    flag_off(FLAG_VIDEO_MODE_ENABLED);
    flag_on(FLAG_VIDEO_MODE_RUNNING);
    flipper_disable ();
    leff_start(LEFF_TURN_OFF_GI);

    shooting_gallery_init(); //all variables are init'ed here

    deff_start_sync (DEFF_SG_INTRO);

    shooting_gallery_engine();


    deff_start_sync (DEFF_SG_END);

    leff_start(LEFF_FLASH_GI);
    leff_start(LEFF_TURN_ON_GI);
    flipper_enable ();
    flag_off(FLAG_VIDEO_MODE_RUNNING);
    flag_off(FLAG_VIDEO_MODE_ENABLED);
} //end of function
Reply all
Reply to author
Forward
0 new messages