Thread: SDL2 - GCC vs. G++ segmentation fault

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    65

    SDL2 - GCC vs. G++ segmentation fault

    I though it was high time I got started with some graphics, so I did, but I've run into an odd issue, which frankly annoys the heck out of me.

    The first line there the problem occur (there may be others, I wouldn't know) is simply:
    Code:
    gScreenSurface = SDL_GetWindowSurface( gWindow );
    Now the real, not so funny issue is that when compiling using GCC, it compiles without issue, but I get a segmentation fault when running the code, while when using G++, I do not.

    Now it may seem that the solution is simple. "Compile with G++ for crying out loud, and stop whining!" True though that may be, I still need to understand what is actually happening, not least because it is my understanding that SDL is written in C, thus this is just plain weird.

    So... Any ideas?


    Best regards.


    edit: tracked down the wrong line of code. Fixed now.
    Last edited by Zacariaz; 05-30-2015 at 04:56 AM.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    65
    For crying out loud, I changes the code, but apparently I didn't... Of course I would like to change it now, but it can't, not can I delete this thread and start a new, nor can I make a duplicate post, a people usually find that annoying...

    Anyway, here's the actual code:

    Code:
    ...
    SDL_Window* gWindow = NULL;
    
    ...
    
    gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, 1280, 720, SDL_WINDOW_BORDERLESS );
    
    ...
    
    if( gWindow == NULL ) // segmentation fault with gcc and not with g++
    I've tried just about everything I can think of, but all my research has achieved is to confirm that there is indeed a difference if you compile with gcc or g++, though usually the roles are reversed, as compared to this case.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    65
    Apparently, when running the program through gdb, the segmentation fault doesn't occur. Either that, or I completely misinterpret the out put, which wouldn't be too surprising, but for now I'll assume that things just keep getting weirder and weirder.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I'm missing the init-function for SDL in your code.
    Your code should have the following fuctions as a mimimum.
    Code:
    …
        SDL_Window* gWindow = NULL;
    
    //Initialize SDL
        if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
            printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
            return 1;
        }
    …
        gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, 1280, 720, SDL_WINDOW_BORDERLESS );
    …
        if( gWindow == NULL ) // segmentation fault with gcc and not with g++
    …
    //Destroy window
        SDL_DestroyWindow( gWindow );
    
    //Quit SDL subsystems
        SDL_Quit();
    …
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    65
    Quote Originally Posted by WoodSTokk View Post
    I'm missing the init-function for SDL in your code.
    Your code should have the following fuctions as a mimimum.
    Code:
    …
        SDL_Window* gWindow = NULL;
    
    //Initialize SDL
        if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
            printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
            return 1;
        }
    …
        gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, 1280, 720, SDL_WINDOW_BORDERLESS );
    …
        if( gWindow == NULL ) // segmentation fault with gcc and not with g++
    …
    //Destroy window
        SDL_DestroyWindow( gWindow );
    
    //Quit SDL subsystems
        SDL_Quit();
    …
    I only posted what seems relevant, as I know where the segmentation fault occur. Here it is in it's entirety, which is nothing really but at standard test case you can get from about any tutorial on the subject:

    Code:
    #include "SDL2/SDL.h"
    #include <stdio.h>
    
    //The window we'll be rendering to
    SDL_Window* gWindow = NULL;
        
    //The surface contained by the window
    SDL_Surface* gScreenSurface = NULL;
    
    
    //The image we will load and show on the screen
    SDL_Surface* gHelloWorld = NULL;
    
    
    const int SCREEN_WIDTH = 1920;
    const int SCREEN_HEIGHT = 1080;
    
    
    int init()
    {
        //Initialization flag
        int success = 1;
    
    
        //Initialize SDL
        if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
        {
            printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
            success = 0;
        }
        else
        {
            //Create window
            gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_BORDERLESS );
            if( gWindow == NULL )
            {
                printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
                success = 0;
            }
            else
            {
                //Get window surface
                gScreenSurface = SDL_GetWindowSurface( gWindow ); // SEGMENTATION FAULT!
            }
        }
        
        return success;
    }
    
    
    int loadMedia()
    {
        //Loading success flag
        int success = 1;
    
    
        //Load splash image
        gHelloWorld = SDL_LoadBMP( "img.bmp" );
        if( gHelloWorld == NULL )
        {
            printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
            success = 0;
        }
    
    
        return success;
    }
    
    
    void close()
    {
        //Deallocate surface
        SDL_FreeSurface( gHelloWorld );
        gHelloWorld = NULL;
    
    
        //Destroy window
        SDL_DestroyWindow( gWindow );
        gWindow = NULL;
    
    
        //Quit SDL subsystems
        SDL_Quit();
    }
    
    
    int main(int argc, char* args[])
    {
        //Start up SDL and create window
        if( !init() )
        {
            printf( "Failed to initialize!\n" );
        }
        else
        {
            //Load media
            if( !loadMedia() )
            {
                printf( "Failed to load media!\n" );
            }
            else
            {
                //Apply the image
                SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
                //Update the surface
                SDL_UpdateWindowSurface( gWindow );
                //Wait two seconds
                SDL_Delay( 2000 );
            }
        }
    
    
        //Free resources and close SDL
        close();
    
    
        return 0;
    }
    I really wish I could delete this thread or at least edit it, as, once again, I've confused my self and others by posting the wrong stuff.

    Where the segmentation fault occur is at the line:
    "gScreenSurface = SDL_GetWindowSurface( gWindow );"
    now proven with finality, but as I mentioned earlier, it does not occur when compiling with g++, as opposed to gcc, which is very confusing.

  6. #6
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Zacariaz View Post
    ...
    Where the segmentation fault occur is at the line:
    "gScreenSurface = SDL_GetWindowSurface( gWindow );"
    now proven with finality, but as I mentioned earlier, it does not occur when compiling with g++, as opposed to gcc, which is very confusing.
    I don't know why you get a seg-fault. I just installed SDL2 on a WinXP SP3 32-bit machine, just to compile your code, using mingw32 v4.8.1:

    Code:
    gcc -std=c99 -g3 -pedantic -Wall -Wextra zacariaz.c -o zacariaz.exe -Ic:/unix/sdl2/include -Lc:/unix/sdl2/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
    It run just fine!

    Perhaps the problem lies in the 64-bit version of the library, compiler and/or runtime.
    "Talk is cheap, show me the code" - Linus Torvalds

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    65
    Thank for trying. As I said, running it through gdb apparently does not cause a seg fsult, so it is really weird.

    Anyway, if it matters, I'm on a x86_64 arch box with proprietary nvidia driver.

  8. #8
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    No problem, most probably I'll need SDL2 anyway

    Dunno why you get that seg-fault. Your code looks ok. Btw, I don't get a sef-fault no matter if I run the exe inside or outside gdb (I also have a nvidia card installed in that machine). Have you tried a different vesrion of gcc and/or mingw? Or have you tried using the 32-bit version of SDL2? It may worth trying any of those.
    "Talk is cheap, show me the code" - Linus Torvalds

  9. #9
    Registered User
    Join Date
    May 2015
    Posts
    65
    i will of course continue to try and figure it out, but for now i think ill try to get some actual work done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL2 Codeblocks error
    By russiancircles in forum Game Programming
    Replies: 2
    Last Post: 05-08-2014, 06:08 AM
  2. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  3. need some help with Segmentation fault
    By zhangz64 in forum C Programming
    Replies: 4
    Last Post: 11-04-2012, 07:02 AM
  4. Segmentation Fault
    By Dansas in forum C Programming
    Replies: 3
    Last Post: 11-21-2008, 02:12 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM