SDL_Quit() freezes at program shutdown

Hello,

I’m currently making a game and an engine using SDL2 and OpenGL and it used to be a simple executable. Now I moved the engine part out into a library so multiple applications can use it. That way I can easily make separate tools using functions provided by the engine. I also moved the actual game part into a separate program. Everything delightfully works just fine. Except one thing: Upon program exit, SDL_Quit() freezes and the program will not continue. Before migrating it to a DLL, it worked just fine. This is my cleanup code:

SDL_FreeCursor(m_pCursor);
SDL_GL_DeleteContext(m_context);
SDL_DestroyWindow(window());
SDL_Quit(); // <-- hangs

The code that does the initializing is below:

if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
    std::cout << "SDL initialized." << std::endl;
}
else
{
    return false;
}

int flags{ 0 };
flags = Config::fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_SHOWN;
flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;

SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

m_pWindow = SDL_CreateWindow("Game", Config::posX == -1 ? SDL_WINDOWPOS_CENTERED : Config::posX, Config::posY == -1 ? SDL_WINDOWPOS_CENTERED : Config::posY, Config::width, Config::height, flags);

if (m_pWindow == nullptr)
    return false;

m_context = SDL_GL_CreateContext(m_pWindow);
if (m_context == nullptr)
    return false;

glewExperimental = GL_TRUE;
auto init_res = glewInit();
if(init_res != GLEW_OK)
{
    std::cout << glewGetErrorString(glewInit()) << std::endl;
}

if( SDL_GL_SetSwapInterval( 1 ) < 0 )   // <-- vsync disabled
{
    printf( "Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() );
}

m_pCursor = init_system_cursor(arrow);
SDL_SetCursor(m_pCursor);

If I try to call SDL_Quit() right after initializing SDL, it works fine, but when I do it at shut down it doesn’t seem to like it.

Can anyone think of a reason why this might be happening?

Thanks :slight_smile:

Matthias

Is there any chance you can run this under a debugger and see what the callstack looks like when it’s frozen? It’d be good to know what part of SDL froze up while deinitializing, as SDL_Quit() touches every subsystem.

Your answer inspired me to build SDL myself, which I previously hadn’t done, so before I had no clue what part of the SDL was causing the problem :smiley:
However, I just did and using those self-built libraries revealed the following call stack:

ISensorManager_SetEventSink does not seem to want to return. I hope that sheds some more light on the issue.

Thanks for your answer :slight_smile:

1 Like

I’ve opened a bug report here, in case we can track this down:

1 Like

So no quick fix, damn :smiley:
So I’ll just comment SDL_Quit() out for now.
Thank you very much for the quick response!

1 Like