2

When I am declaring a new member attribute in my game.cpp class like this:

    int test;

I have a stack smashing error which is really strange because without the new attribute my program is running fine. I know my problem is probably somewhere else in my program and I will not copy my whole program but some help about how to make the message more explicit and maybe some advice about stack smashing debugging would be nice. This is my game.cpp class in case. Sorry if I am missing something important I am a beginner in c++. Thank you

My error:

* stack smashing detected *: terminated Aborted (core dumped)

Game.cpp

public:
    std::vector<Ball> m_balls; //list of ball in the current game, in case ball split in 3 energy ball
    std::vector<Vault> m_vaults; //list of vaults , in case we are in cooperative mode
    std::vector<Wall> m_walls; // lists of walls
    int test; //Stack smashing wtf
    Sdl_o_surface m_bg; // image containing sprites
    Sdl_o_window m_window; //window containing one or more games (for the moment 1 or 2)
    int score = 0;
    int m_current_level = 1;
    int m_x1; // borders of the game
    int m_x2;
    int m_y1;
    int m_y2;

    //constructor
    Game(int x1, int x2 , int y1, int y2, int mode, Sdl_o_surface s,  Sdl_o_window w);

    //methods
    void initSolo();
    void initCoop();
    void refreshWindowAndObjects();
    void startGame();
    void updatePosition();
    void wallsCollision(Ball &ball);
    void borderCollision(Ball &ball);
    void vaultCollision(Ball &ball);
    Sdl_o_rectangle getTexturePosition(); //get texture position depending on current level
    Sdl_o_rectangle getBorders();
    void parseLevelText();
    void updateVaultsPosition(int x, int y);

    //wall section
    void chooseWallType(char type);
    void placeWall(char t);
Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
Jules
  • 185
  • 1
  • 16
  • 2
    You typically get "stack smashing" errors because you write out of bounds of an array defined locally in a function. I suggest [learning how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and how to use tools such as [Valgrind](http://valgrind.org/). – Some programmer dude Nov 15 '18 at 08:03
  • 2
    Most likely you have a buffer overrun somewhere. Look through your code for anywhere that you're accessing arrays on the stack, pointers to stack variables, or even returning pointers/references to local variables. Maybe try an analysis tool like valgrind. Turn up your compiler warnings all the way. – paddy Nov 15 '18 at 08:03
  • I'll try to find where it come from but what i find really strange is that i have this error only when i'm declaring a new attribute for this class, otherwise my program is running fine. I don't know if you ever had similar case because declaring a new attribute with a primitive type is not that dangerous/special isn't it ? – Jules Nov 15 '18 at 08:08
  • What platform and tools are you using to debug with ? – darune Nov 15 '18 at 08:12
  • 3
    Welcome to the wonderful world of [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). Where things sometimes seems to work, and in the next you get a horde of [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html). – Some programmer dude Nov 15 '18 at 08:14
  • I'm on ubuntu 18.04, currently debugging with valgrind and my compiler is gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3) – Jules Nov 15 '18 at 08:15
  • You may get to the root of the issue faster if you examine memory contents, or plant 'detector' variables with known values in your stack and check their value regularly. Make sure you have the current broken code saved somewhere safe (in source control or a backup copy) so you can easily get back to the broken code if you accidentally "unbreak" it and can't find your way back. – paddy Nov 15 '18 at 08:36
  • 2
    @Jules - unfortunately, what you describe (symptoms changing or disappearing after defining a variable - which you're calling an "attribute") is *typical* when there is some code doing pointer molestation (dereferencing a NULL, falling off the end of an array, returning a pointer or reference to a variable that then passes out of scope, using dynamically allocated memory after release, etc etc). There are so many potential causes, and interactions, that it takes a lot of effort to find the cause. Symptoms may also change/disappear when changing optimisation or debugging settings. – Peter Nov 15 '18 at 08:37
  • @Jules, you mentioned using valgrind; if that doesn't catch the issue, give the undefined behavior sanitizer ("ubsan") and address sanitizer ("asan") a try. These are easily turned on with the gcc flags `-fsanitize=undefined` or `-fsanitize=address`, respectively. (Just make sure you pass that flag to both the compiler and linker.) – s3cur3 Nov 15 '18 at 18:38
  • Thanks for all the replies, i think i fixed all the memories leak using valgrind, I don't have any more error. Valgrind was indicating more than 1700 error and I reduced them to 80 and I had no more issue. The lasts memory leak come from a library i'm using so i can't really do much about it. It was interesting, thanks ! – Jules Nov 15 '18 at 21:52

0 Answers0