How can I find out where and why this segmentation error occurs?

I'm having trouble figuring out a problem with my code ... I am in the early stages of prototyping the game (my first major project). It often, but not always, falls with a segmentation error. Here's the program flow ...

  • title screen - press z to start a new game.
  • the player and the enemy on the screen ... the enemy takes life, when faced, there are 4 lives.
  • when the nature of life is 0, the game returns to the title screen - press z to start a new game.

The last step is where the crash happened ... The defeat only seems to happen after the player dies and returns to the title screen and the player press z to start a new game, but this is not always a crash. Through gdb, I decided that this happens when the deconstructor for Title is called ...

Here is the debugging information and the corresponding code ... I will provide any other code / information upon request.

*** glibc detected *** /home/rzrscm/code/demogamething/game: free(): invalid pointer: 0x080c6b98 ***

//memory map info omitted

0xb7c54537 in raise () from /lib/libc.so.6
(gdb) where
#0  0xb7c54537 in raise () from /lib/libc.so.6
#1  0xb7c57922 in abort () from /lib/libc.so.6
#2  0xb7c8afbd in ?? () from /lib/libc.so.6
#3  0xb7c950ca in ?? () from /lib/libc.so.6
#4  0xb7c96918 in ?? () from /lib/libc.so.6
#5  0xb7c99a5d in free () from /lib/libc.so.6
#6  0xb7f4e776 in SDL_FreeSurface () from /usr/lib/libSDL-1.2.so.0
#7  0x0804ac7f in ~Title (this=0x80b4250, __in_chrg=<value optimized out>) at title.cpp:13
#8  0x08049d3a in GameState::load (this=0x804e368, loadState=LEVEL) at gamestate.cpp:39
#9  0x08049c5c in GameState::change (this=0x804e368, changeTo=LEVEL) at gamestate.cpp:26
#10 0x08049753 in Player::handleEvent (this=0x804e300) at player.cpp:102
#11 0x080490c8 in main () at main.cpp:27

So, I know that a failure occurs when it changes states from TITLE to LEVEL ... Here is a class function that unloads and loads states ... currentState is a private vector in the GameState class ...

std::vector<GameState *> currentState;

void GameState::load(STATE loadState) {
    if(state == TITLE) {
        while(!currentState.empty()) {
            delete currentState.back();
            currentState.pop_back();
        }
        currentState.push_back(new Title()); 
    }
    else if(state == LEVEL) {
        while(!currentState.empty()) {
            delete currentState.back();
            currentState.pop_back();
        }
        currentState.push_back(new Level(currentLevel));
   }
}

A crash occurs when the deconstructor for the Title class is called ... This happens regardless of whether it frees the music or image ... Whatever the first function, it fires.

Title::~Title() {
    SDL_FreeSurface(background);
    Mix_FreeMusic(music);
    background = NULL;
    music = NULL;
}

...

SDL_Surface *loadImage(std::string imageFile) { 
    SDL_Surface *loadedImage;
    SDL_Surface *newImage; 

    loadedImage = IMG_Load(imageFile.c_str()); 
    newImage = SDL_DisplayFormatAlpha(loadedImage); 

    SDL_FreeSurface(loadedImage); 

    return newImage; 

}

[edit] , , , ... , ...

0xb7c54537 in raise () from /lib/libc.so.6
(gdb) backtrace
#0  0xb7c54537 in raise () from /lib/libc.so.6
#1  0xb7c57922 in abort () from /lib/libc.so.6
#2  0xb7c8afbd in ?? () from /lib/libc.so.6
#3  0xb7c950ca in ?? () from /lib/libc.so.6
#4  0xb7c9633e in ?? () from /lib/libc.so.6
#5  0xb79974e2 in ?? () from /usr/lib/libmikmod.so.2
#6  0xb7997640 in Player_Free () from /usr/lib/libmikmod.so.2
#7  0xb7ebb6e3 in Mix_FreeMusic () from /usr/lib/libSDL_mixer-1.2.so.0
#8  0x0804ac8d in ~Title (this=0x80c6bc0, __in_chrg=<value optimized out>) at title.cpp:14
#9  0x08049d3a in GameState::load (this=0x804e368, loadState=LEVEL) at gamestate.cpp:39
#10 0x08049c5c in GameState::change (this=0x804e368, changeTo=LEVEL) at gamestate.cpp:26
#11 0x08049753 in Player::handleEvent (this=0x804e300) at player.cpp:102
#12 0x080490c8 in main () at main.cpp:27
+3
1
#5  0xb7c99a5d in free () from /lib/libc.so.6
#6  0xb7f4e776 in SDL_FreeSurface () from /usr/lib/libSDL-1.2.so.

, , .

free(): invalid pointer: 0x080c6b98 

, , . background ?

+2

All Articles