Lazzo
Lazzo

Reputation: 23

SDL - Segmentation Fault (core dumped), any thoughts?

Having this problem since I've installed SDL. First of all, I've tried to install it with the tar.gz file, didn't went ok when trying to compile (terminal couldn't find the dir for SDL lib), so after that I've installed the synpatic pack mng, and sucessfully downloaded the "libsdl1.2-dev" file. I am following lazzy foo's tutorial for SDL, whenever I try to compile a simple code to create a screen and blit an image, i get the following message in the terminal:

(gcc -Wall -o teste teste.c -lSDL -lSDL_image)

"Segmentation fault (core dumped)"

Here it is my code in C:

    #include <stdio.h>
    #include <stdlib.h>
    #include "SDL/SDL.h" 

    int main( int argc, char* args[] ) 
    {
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

    if (screen == NULL) {
        printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
        exit(1); /* Unrecoverable error */
    }

    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);

    SDL_Delay(2000);

    SDL_FreeSurface(hello);

    SDL_Quit();

    return 0;
    }

Here's a log using gdb to backtrace:

LOG

GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from teste...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/lazzo/Documentos/Treino/teste 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff707c700 (LWP 5605)]

Program received signal SIGSEGV, Segmentation fault.
SDL_Flip (screen=0x0) at ./src/video/SDL_video.c:1109
1109    ./src/video/SDL_video.c: No such file or directory.
(gdb) bt
#0  SDL_Flip (screen=0x0) at ./src/video/SDL_video.c:1109
#1  0x00000000004009a2 in main ()
(gdb) c
Continuing.
[Thread 0x7ffff7fd8740 (LWP 5601) exited]

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) q
]0;lazzo@J-Ubuntu: ~/Documentos/Treinolazzo@J-Ubuntu:~/Documentos/Treino$ exit
exit

END OF LOG

Any help you guys could give me would be really appreciated, and I apologize for my bad english, I am from Brazil and still learning english.

UPDATE

After adding Klas suggestion to my code, I've got this from terminal:

"SDL_SetVideoMode failed: No avaible video device"

How is that even possible? (my videocard is a radeon HD 4850 btw)

Upvotes: 0

Views: 1142

Answers (2)

Lazzo
Lazzo

Reputation: 23

Only thing that have worked out in my case was to format Ubuntu and try another distro. Right now I am using Linux Mint, and despite that fact that it's totally based on Ubuntu, everything is working as expected now. Just sharing my solution to the problem, in case somebody else have this very same problem someday.

Upvotes: 0

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33273

Problem round 1 (compilation):

The target filename must follow immediately after the -o option, so you should change the order of the arguments:

gcc -Wall -o teste teste.c -lSDL -lSDL_image

This may not solve all your build problems, but it is a good start.

Problem round 2 (adding error handling):

The call to SDL_SetVideoMode returned null. If you get a return value of null you should call SDL_GetError immediately after to check what the error is:

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if (screen == NULL) {
    printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
    exit(1); /* Unrecoverable error */
}

You should add similar handling for the other SDL calls.

Upvotes: 2

Related Questions