2012-03-06 8 views
0

私が書いているゲームのためにChipmunkという物理ライブラリを使用しています。ライブラリコールのセグメンテーションフォルト

私のinitialize関数では、グローバル変数cpSpace spaceを初期化します。その後、私はcpSpaceStep(space, timestep)と更新します。この関数のプロトタイプはvoid cpSpaceStep(cpSpace *space, cpFloat dt);です。私はこの関数呼び出しでセグメンテーションを取得しています。私はこれらの2つの関数呼び出しを以下のコードでマークしました。

完全なコードは以下の通りです:あなたのcpInitChipmunk()コールはどこ

#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出力を投稿できます。 –

+0

解決策は、 'gdb'出力から明白になるはずです。 –

+0

'cpSpaceNew()'は何を返しますか?ポインタまたは参照? –

答えて

1

ですか?それがなければ、cpSpaceNew()はNULL(または迷惑メール)をうまく返す可能性があります。

簡単に確認できます。すぐにcpSpaceNew()を呼び出した後、挿入します。値が何であるかを確認するために、

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

(または何か同等の

また、単に、同様にそれを使用しようとする前にそのimmediatleyを行うことをお勧めしますケースには何かがあなたがcpSpaceStep()を呼び出すときspaceがNULLであるため、

+0

あなたが提案した場所にいくつかのprintfを追加し、cpInitChipmunk()を追加しました。私がprintfから得た出力は、2つの同一のメモリアドレスでした。したがって、それは適切に初期化され、壊れていません。 –

+0

initコールを追加する前に値を確認しましたか(まだクラッシュしていますか)?デバッグ101では、一度に_one_もののみを変更するように指示しています。そして、何が印刷されたポインタ値ですか? – paxdiablo

+0

私はinit呼び出しを取り出し、コードを実行しました。ポインタのために015D184を得ました。 initが追加され、再コンパイルされ、01711840が取得されました。まだクラッシュしています。 –

0

があることでした。それが破損します。そしてcpSpaceStep()機能で、試みがNULLあるポインタ間接参照に行われます。spaceが正しくに初期化されているかどうかをチェックします。

+0

paxdiabloと私の議論のコメントを参照してください。それはヌルptrではありません。 –

関連する問題