Segmentation Fault weirdness

Hi, when I run my executable, I get a Segmentation Fault and it crashes when my
event loop is written like so:

while(done==0){
SDL_Event event;

while(SDL_PollEvent(&event)){
	if(event.type==SDL_QUIT) done=1;
}

keys=SDL_GetKeyState(NULL);

if(keys[SDLK_UP]) y-=1;
if(keys[SDLK_RIGHT]) x+=1;
if(keys[SDLK_DOWN]) y+=1;
if(keys[SDLK_LEFT]) x-=1;
if(keys[SDLK_ESCAPE]) done=1;

drawframe();

}

…but if I insert “printf(“whatever”);” between "keys=SDL_GetKeyState(NULL);"
and “if(keys[SDLK_UP]) y-=1;” the program runs smoothly until I close it and I
get another Segmentation Fault. I’m running Slackware 10 with stock gcc and SDL.
The code is basically a rewrite of the 2nd lesson over at cone3d, but when I
compile the code from the tutorial, everything works without a hitch… I’m
seriously stumped by this…

mo wrote:

Hi, when I run my executable, I get a Segmentation Fault and it crashes when my
event loop is written like so:

while(done==0){
SDL_Event event;

while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT) done=1;
}

keys=SDL_GetKeyState(NULL);

if(keys[SDLK_UP]) y-=1;
if(keys[SDLK_RIGHT]) x+=1;
if(keys[SDLK_DOWN]) y+=1;
if(keys[SDLK_LEFT]) x-=1;
if(keys[SDLK_ESCAPE]) done=1;

drawframe();
}

…but if I insert “printf(“whatever”);” between "keys=SDL_GetKeyState(NULL);"
and “if(keys[SDLK_UP]) y-=1;” the program runs smoothly until I close it and I
get another Segmentation Fault. I’m running Slackware 10 with stock gcc and SDL.
The code is basically a rewrite of the 2nd lesson over at cone3d, but when I
compile the code from the tutorial, everything works without a hitch… I’m
seriously stumped by this…

So, chances are you have a bug in your code (yay, this happens).
Why don’t you send (or send a link to) a whole file that we can compile
and test here ?

Stephane

Ok, here is a tgz of the files: http://thesilent1.freeownhost.com/sdl003.tgz .
I compile it with this command:
gcc sdl003.c -o sdl003 sdl-config --cflags --libs
but a Makefile is included.

In your SDL_utils.h:

void uDrawSurface(SDL_Surface* src,SDL_Surface* dest,int x,int y,int
w,int h,int x2,int y2){
SDL_Rect* scoord;
SDL_Rect* dcoord;

You never initialize those two pointers, so when you set scoord->x/y/w/h
and dcoord->x/y you end up corrupting memory.

-Willem JanOn Sat, 2005-01-22 at 18:48 +0000, mo wrote:

Ok, here is a tgz of the files: http://thesilent1.freeownhost.com/sdl003.tgz .
I compile it with this command:
gcc sdl003.c -o sdl003 sdl-config --cflags --libs
but a Makefile is included.

mo wrote:

Ok, here is a tgz of the files:
http://thesilent1.freeownhost.com/sdl003.tgz . I compile it with
this command: gcc sdl003.c -o sdl003 sdl-config --cflags --libs
but a Makefile is included.

You should always compile with -Wall to enable warnings; they tend to
reveal things you ought to know.–
CalcRogue: TI-89, TI-92+, PalmOS, Windows and Linux.
http://calcrogue.jimrandomh.org/

Note that, despite the name, -Wall doesn’t give you anywhere
near “all” of the warnings you could use. I use this:

PKG_CFLAGS := -fno-common -ffast-math
-W -Wall -Wshadow -Wcast-align -Wredundant-decls
-Wbad-function-cast -Wcast-qual -Wwrite-strings -Waggregate-return
-Wstrict-prototypes -Wmissing-prototypes
#CFLAGS := -O2 -s # best warnings, compact, fast
CFLAGS := -O1 -ggdb # good debugger support
ALL_CFLAGS := $(PKG_CFLAGS) $(CFLAGS)

Without that -O2 you won’t get all the warnings. The compiler
simply won’t bother to do all the analysis needed.On Sat, 2005-01-22 at 23:42, jimrandomh wrote:

mo wrote:

Ok, here is a tgz of the files:
http://thesilent1.freeownhost.com/sdl003.tgz . I compile it with
this command: gcc sdl003.c -o sdl003 sdl-config --cflags --libs
but a Makefile is included.

You should always compile with -Wall to enable warnings; they tend to
reveal things you ought to know.