Search code examples
c++vectorstack-overflow

Vector & Stack Overflow


To explain my question:

I have a simple vector array titled enemies, that holds objects of Enemies. In my level selection menu for the levels i have in-game, when the player clicks on a level i push back all the enemies for that given level into the vector array.

Currently i only have 3 levels fully done with all their enemies, today when trying to-do the forth level, now i keep getting a stack overflow. And i'm not sure why this is happening, i mean surely the array is only ever assigned anything when i reach first of all reach the level selection menu and then press anything on the keyboard, but no straight away it crashes.

So in terms of pseduo code, here's what i do:

switch(keyboardInput)
    //ALL keyboard button specific input
    default:
        if (levelSelected == 1)
              //push back level 1 enemies into vector
        elseif (levelSelected == 2)
             //push back level 2 enemies into vector etc. etc.

In terms of numbers that are being push into the vector array:

Level 1 = 26 enemies

Level 2 = 113 enemies

Level 3 = 204 enemies

Level 4 = 38 enemies (Not even half way finished with creating this level)

Total = 381 enemies.

Commenting out a few of the vector push backs that i has just added for the 4th level then has the game running fine. But obviously this is a issue that i need to overcome to allow me to have enemies assigned to the levels.

It crashes on the file "chkstk.asm for reference. So i'm a bit confused by this, why is exactly is this happening is my main question? And hopefully how should i overcome this issue?

Any help greatly appreciated!

Edit: So to answer some of the questions, i don't use any sort of recursion in my code and for a greater view of the error, here's a screenshot

Error Returned

I am loading in total 86 images, and 3 fonts. But even commenting a few of those out I still get the same error message above. The only other thing of any note-worthy would be the actually Enemies class itself. But specifically all that holds is a few variables and a few animations. In total there's 4 animations available per enemy, some have the animation some don't. I'll post the animation class below; http://pastebin.com/X3GUgJiJ - Header

http://pastebin.com/pL1iEkiy - .cpp content.

Even then though im not sure that there's anything significantly memory hogging there.

Edit Two: So here's a link to to my level selection code, its quite large. Most of the code itself is game-related to the level selection screen and too rendering the correct imagery. http://txtup.co/YLVRo

Edit Three: Commented out all the enemies being pushed back into the vector array leads to surprising results, the level loading code throws up an error, could this just be in result to whatever else is messing up the stack or is it infact the loading code that is messing up the stack? Im not sure. But here's an image of the level error: https://i.sstatic.net/H3Mr1.jpg

Edit Four: So after a long task of looking through the code, see what perhaps could be causing the issue i think i may have a theory; So each Enemy object has the possibility of having/using 4 different animations. 5 really but its a long story, there's basically 2 different versions of one animation for different sized enemies. So if we place that into some figures, these animations are their own class titled Animation. Is it that For every enemy object pushed back into the array will contain its own instance of all the specific Animations that are available, and thus as time has gone on and i've added in more and more enemies the issue unbeknown to me has grown worse and worse.

I mean in total that would be 1524 Animation instances, currently i pretty much commented most of the code out. Most of the big things i thought may have been causing the issue, slowly i'm un-commenting the lines trying things, making sure that this in-fact correct. Problem remains now that what should be done?


Solution

  • There are only three potential causes of stack overflow:

    1. Endless/excessive function recursion.
    2. Allocating excessively large objects on the stack.
    3. Overwriting a stack frame link pointer.
      • May result from buffer overflow of an array on the stack
      • or using an expired pointer or reference to a local variable.

    None of these can conceivably result from misuse of a vector. Probably something related to the push_back is aggravating a preexisting problem.

    If you want to bring out the big guns, try Valgrind. But it looks like you need to boil things down and trim away unessential details. Don't be so concerned with particular numbers. 1524 animation instances should be just the same as 2 if the program is written properly.