0

I'm getting errors in my program between two (theoretically) consecutive lines of code, and have no idea what could cause this.

My whole code is huge, so here's the basics;

int playRoom(std::string currentRoom = "Storage_room", std::string entryDoor = "NULL"){
    log("Starting playRoom()");

    // code to play the level
    // includes setting up box2d world
    // and playing the level

    if(playerWantsRestart){
        log("Restart level");
        return playRoom(savedData.roomName, savedData.entryDoor);
    }

    log("Leaving playRoom()");
    return 0;
}

int main( int argc, char* args[] ){
    // Set up SDL etc..
    playRoom();
    log("Back in main()");
    // Close SDL
    return 0;
}

If I never use the restart option, everything is fine. If I do use it, the program exits with status 3 and the log file reads:

Starting playRoom()
Restart level
Starting playRoom()
Leaving playRoom()

So the error appears to be in "return 0;"?? I don't think status 3 is an overflow, and it's only recursed (?) once anyway, so... I'm using Codeblocks 12.11, compiling with GNU GCC. Any help or ideas would be great!

  • 2
    SDL tends to exit(3) when you use its API incorrectly, like using invalid surfaces. I recommend stepping through the last few calls of your program before the crash using gdb to see what is causing it. You can do that through code::blocks' debugger. – Nicolas Louis Guillemot Sep 06 '13 at 19:57
  • You don't show a lot of code, but in addition to Nicolas' comment, there may be issues with reentering the function without having executed the destructors of local objects of the previous iteration yet. I recommend placing the code that actually does the playing in a separate function, and change your existing `playRoom()` function to use a regular loop to call that separate function. – jxh Sep 06 '13 at 20:05

2 Answers2

0
int playRoom(std::string currentRoom = "Storage_room", std::string entryDoor = "NULL")

Why are you using int's? I would use Boolean.

Either way read this.

You are calling two different types of methods not sure if they updated java to include this syntactic sugar yet. I've been out of the loop but.

playRoom();

This function should have a set currentRoom and an entryDoor in the method by default.

Then overload it with.

playRoom(string currentRoom , string entryDoor);
Community
  • 1
  • 1
progrenhard
  • 2,333
  • 2
  • 14
  • 14
0

Try removing the return statement from the if condition.