库调用时出现分段错误 [英] Segmentation fault on library call
问题描述
我正在使用一个名为Chipmunk的物理库来开发我正在写的游戏。
I'm using a physics library called Chipmunk for a game that I'm writing.
在我的初始化
函数中,我初始化全局变量 cpSpace space
。然后在更新中,我调用 cpSpaceStep(space,timestep)
。此函数的原型为 void cpSpaceStep(cpSpace * space,cpFloat dt);
。我在此函数调用中遇到段错误。我在下面的代码中标记了这两个函数调用。
In my initialize
function I initialize the global variable cpSpace space
. Then in update I call cpSpaceStep(space, timestep)
. The prototype for this function is void cpSpaceStep(cpSpace *space, cpFloat dt);
. I'm getting a segfault on this function call. I've marked those two function calls in the code below.
完整代码如下:
#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() );
}
}
}
推荐答案
您的 cpInitChipmunk()
通话在哪里?否则, cpSpaceNew()
很可能返回NULL(或垃圾)。
Where's your cpInitChipmunk()
call? Without that, it's likely that cpSpaceNew()
may well return NULL (or junk).
易于检查。在调用 cpSpaceNew()
之后,立即插入:
Easy enough to check. Immediately after the call to cpSpaceNew()
, insert a:
printf ("%p\n", space);
(或类似的东西,看值是什么。
(or something equivalent, to see what the value is.
您可能还想立即尝试使用它,以防万一某些东西损坏了它。
You may also want to do that immediatley before trying to use it as well, just in case something corrupts it.
这篇关于库调用时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!