-1

Every time I compile my simple SDL1.2 code it's compiled successfully but when I try to run it via terminal (alt+t in Ubuntu):

./game
Segmentation fault (core dumped)

I get this error. Can you help please? This is the code:

#include<SDL/SDL.h>

int main(int argc,char args)    
{
    SDL_Init( SDL_INIT_EVERYTHING);    
    SDL_Surface* screen;        
    screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE);
    SDL_Flip(screen) ![problem running the program][1];
    SDL_Delay(5000);    
    SDL_FreeSurface(screen);    
    SDL_Quit();
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
Amir Bennasr
  • 216
  • 1
  • 2
  • 10

4 Answers4

1

SDL_SetVideoMode returns NULL on error which you do not check for.

StenSoft
  • 9,369
  • 25
  • 30
1

Since you're running this via a terminal, I suspect you may have forgotten to tell Xorg to allow running from it. In fact, if this is really the problem it'll prevent any program from running when started that way.

To fix the problem, enter this into the terminal (this only needs to be done once per session):

xhost +

You should get a message that it was successful. I cannot recall the exact message, but it is something like this:

Clients are now allowed to connect from any host.

What was happening (assuming that I was correct regarding xhost) was that the SDL_SetVideoMode() call was failing and returning NULL, because Xorg rejected the connection. Since you're not checking for that, SDL_Flip() ended dereferencing a NULL pointer --- hence the segfault.


SIDE-NOTE: There is an error in your code, however --- namely, you should not call SDL_FreeSurface(screen);; that particular surface is special, and is freed by SDL_Quit(); automatically. Source (see "Return Value" section): http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlsetvideomode.html

Tim Čas
  • 10,501
  • 3
  • 26
  • 32
0

Run it under valgrind. Or GDB. Or some other debugger of your choice.

You should probably be successfully allocating memory for screen.

Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30
  • `SDL_SetVideoMode` allocates the memory for you (and you should *NOT* allocate it yourself --- unless you want a memory leak), so your answer is incorrect. – Tim Čas Feb 10 '15 at 01:39
  • Calling SDL_SetVideoMode _is_ allocating memory yourself. Which returns NULL in the time honoured tradition of near-fatal memory allocation errors. I'll add the word "successfully" to the answer. – Jon Chesterfield Feb 10 '15 at 07:22
  • Well, depends on the perspective; generally, when an API allocates the memory behind your back, it's usually said that you are *not* allocating it yourself (doing it yourself would mean having it on the stack *or* using `malloc` and then passing a pointer). Your original answer implied you meant this. At any rate, this does not resolve his core problem --- namely, that the window creation is failing. – Tim Čas Feb 10 '15 at 11:39
  • To add to the above: If you say `SDL_SetVideoMode` is allocating memory by yourself, then there's no point in saying that. Otherwise, *EVERYTHING* is allocating it yourself (since, when you go back up the stack, you'll *eventually* hit your own code; even if you're currently somewhere deep within the kernel because of a system call). – Tim Čas Feb 10 '15 at 11:49
0

Check if SDL_SetVideoMode() failed!

screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
if (screen == NULL) /* error  */;
pmg
  • 106,608
  • 13
  • 126
  • 198
  • if (screen == NULL) /* error */; printf("erreur") ; when i re-run the program it works fine wait for 5 seconds then the screen show me erreur then there is an error loading the screen – Amir Bennasr Feb 09 '15 at 19:46
  • `if (screen == NULL) { fprintf(stderr, "erreur"); exit(EXIT_FAILURE); }` – pmg Feb 09 '15 at 19:57