Search code examples
cgraphicssegmentation-faultsdl

SDL_Quit segmentation fault


I'm on Ubuntu 22.04.4 LTS, and I'm getting into using the SDL2 library, but for some reason calling SDL_Quit() gives me a segmentation fault. Here's the minimum amount of code for which the error occurs:

#include <SDL2/SDL.h>

int main(void)
{                                                                
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *screen = SDL_CreateWindow("test", 100, 100, 680, 550, 0);
    SDL_DestroyWindow(screen);
    SDL_Quit();
    return 0;
}

Compiled using:

gcc test.c `sdl-config --cflags --libs` -lSDL2main -lSDL2 -Wall -pedantic -std=gnu99 -o test

If I don't call SDL_CreateWindow() it works for some reason. I ran it in gdb and backtraced it, it seems the error occurred in a function called ___pthread_mutex_lock(). Does anyone have any idea what is happening here?


Solution

  • I'm guessing since you are compiling against SDL2, you should be using sdl2-config instead of sdl-config.

    To debug, you should try viewing those tags independently by typing in a shell MY_FLAGS=$(sdl2-config --cflags --libs) and then echo $MY_FLAGS. You should have something like

    -I/include/path/to/SLD2 -D_REENTRANT -lSDL2
    

    with maybe some other flags.

    I managed to compile and run your code without errors on Ubuntu 20.04 (it creates a window and closes it instantly).