2012-03-06 30 views
0

我正在使用名爲Chipmunk的物理庫來製作我正在寫的遊戲。庫調用中的分段錯誤

在我的initialize函數中,初始化全局變量cpSpace space。然後在更新中我打電話cpSpaceStep(space, timestep)。這個功能的原型是void cpSpaceStep(cpSpace *space, cpFloat dt);。我在這個函數調用中遇到了段錯誤。我在下面的代碼中標記了這兩個函數調用。

完整的代碼如下:

#include "../include/SDL/SDL_image.h" 
#include "../include/SDL/SDL.h" 
#include "../include/Player.h" 
#include "../include/Timer.h" 
#include "../include/Block.h" 
#include "../include/ImageLoader.h" 
#include "../include/chipmunk/chipmunk.h" 
#include <string> 

//Screen attributes 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 
const int SCREEN_BPP = 32; 

//The frame rate 
const int FRAMES_PER_SECOND = 60; 

SDL_Event event; 
SDL_Surface *screen = NULL; 
SDL_Surface *player_img = NULL, *block_img = NULL; 
Player *player; 
Timer fps; 
cpSpace *space; 

bool quit = false; 

void initialize(); 
void update(); 

int main(int argc, char* argv[]) 
{ 
    initialize(); 
    update(); 

    return 1; 
} 

void initialize() 
{ 
    //Initialize all SDL subsystems 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) 
    { 

    } 

    //Set up the screen 
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); 

    //If there was an error in setting up the screen 
    if(screen == NULL) 
    { 

    } 

    //Set the window caption 
    SDL_WM_SetCaption("Move the Dot", NULL); 

    cpVect gravity = cpv(0, 100); 

//******************cpSpacenew()*****************  
//This is where space is init'ed 
    space = cpSpaceNew(); 
//*********************************************** 


} 

void update() 
{ 
    //While the user hasn't quit 
    while(quit == false) 
    { 
     //Start the frame timer 
     fps.start(); 

     while(SDL_PollEvent(&event)) 
     { 
      //Handle events for the dot 
      player->handle_input(&event); 

      //If the user has Xed out the window 
      if(event.type == SDL_QUIT) 
      { 
       //Quit the program 
       quit = true; 
      } 
     } 

     player->update(); 

     cpFloat timeStep = 1.0/FRAMES_PER_SECOND; 

    //************************Segfault********************************************** 
     cpSpaceStep(space, timeStep); 
    //****************************************************************************** 

     //Cap the frame rate 
     if(fps.get_ticks() < 1000/FRAMES_PER_SECOND) 
     { 
      SDL_Delay((1000/FRAMES_PER_SECOND) - fps.get_ticks()); 
     } 
    } 
} 
+0

如果需要,我可以發佈gdb輸出。 – 2012-03-06 05:15:13

+0

該解決方案應該從'gdb'輸出中顯而易見。 – 2012-03-06 05:18:02

+0

'cpSpaceNew()'返回什麼?一個指針或引用? – 2012-03-06 05:19:22

回答

1

哪裏是你的cpInitChipmunk()電話嗎?沒有這個,很可能cpSpaceNew()可能會返回NULL(或垃圾)。

容易檢查。調用cpSpaceNew()後,立即插入:

printf ("%p\n", space); 

(或東西等同,看到值是什麼

你也可以嘗試使用它,以及之前做到這一點immediatley,公正。在一些情況下,它破壞。

+0

我在你建議的位置添加了幾個printf,並添加了cpInitChipmunk()。我從printf獲得的輸出是兩個相同的內存地址。所以它被正確初始化並且沒有被破壞。 – 2012-03-06 05:29:33

+0

您是否檢查了添加init調用的值_before_(它是否仍然崩潰)?調試101要求您一次只更改_one_。而且,印刷的指針值是什麼? – paxdiablo 2012-03-06 05:43:46

+0

我拿出init調用並運行代碼。指針有015D184。增加了init,重新編譯,得到01711840.它仍然崩潰。 – 2012-03-06 05:46:49

0

可能是因爲space爲NULL,當你調用cpSpaceStep()。而在cpSpaceStep()功能,嘗試進行取消引用指針是NULL檢查是否space是正確初始化。

+0

請參閱我與paxdiablo的討論中的評論。這不是一個空ptr。 – 2012-03-06 06:51:50