简体   繁体   English

如何确定此分段故障发生的位置和原因?

[英]How can I figure out where and why this segmentation fault occurs?

I'm having trouble figuring out the problem with my code...I'm in the early stages of prototyping a game (my first serious project). 我无法用我的代码搞清楚问题......我正处于游戏原型设计的早期阶段(我的第一个认真的项目)。 It frequently, but not always, crashes with a segmentation fault. 它常常(但不总是)因分段故障而崩溃。 Here's the flow of the program... 这是程序的流程......

  • title screen - press z to start new game 标题屏幕 - 按z开始新游戏
  • player and enemy on screen...enemy takes a life when collided with, there are 4 lives. 玩家和敌人在屏幕上...敌人在与之相撞时生活,有4条生命。
  • when character's life is 0, game goes back to title screen - press z to start new game 当角色的生命为0时,游戏返回标题画面 - 按z开始新游戏

The last step is where the crash occurs...The crash only seems to happen after the player dies and is brought back to the title screen and the player presses z to start another game, but it doesn't always crash. 崩溃发生的最后一步......崩溃似乎只发生在玩家死亡并被带回标题屏幕并且玩家按z开始另一个游戏,但它并不总是崩溃。 Through gdb, I've determined that it happens when the deconstructor for Title is called... 通过gdb,我确定它会在调用Title的解构函数时发生...

Here's the debug info and relevant code...I'll provide any other code/info if requested. 这是调试信息和相关代码......如果需要,我会提供任何其他代码/信息。

*** 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, what I know is that the crash occurs when it's changing states from TITLE to LEVEL...Here's the class function that unloads and loads the states...currentState is a private vector in the GameState class... 所以,我所知道的是崩溃发生在它将状态从TITLE更改为LEVEL时...这是卸载和加载状态的类函数... currentState是GameState类中的私有向量...

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));
   }
}

The crash happens when the deconstructor for the Title class is called...It happens whether it's freeing the music or the image...Whichever one is the first function is the one it crashes on. 当调用Title类的解构函数时会发生崩溃......无论是释放音乐还是图像,它都会发生......无论哪个是第一个函数,它都会崩溃。

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

Here's the code for the image loading function... 这是图像加载功能的代码......

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] I ran it through the debugger a couple more times without making changes to the code since I still can't find what's wrong...And each time the crash occurred while trying to free the music... [编辑]我在调试器中运行了几次而没有对代码进行更改,因为我仍然无法找到错误的内容......每次尝试释放音乐时发生崩溃......

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
#5  0xb7c99a5d in free () from /lib/libc.so.6
#6  0xb7f4e776 in SDL_FreeSurface () from /usr/lib/libSDL-1.2.so.

You are probably freeing a pointer which is invalid. 你可能正在释放一个无效的指针。 Now I read 现在我看了

free(): invalid pointer: 0x080c6b98 

You might be trying to free an object which was not allocated dynamically. 您可能正在尝试释放未动态分配的对象。 How does background get his value? background如何获得他的价值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM