Skip to content

Instantly share code, notes, and snippets.

@Fulgen301
Last active March 6, 2023 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fulgen301/232dcac99c7c7f2fd4a9e5c0d7df7ce2 to your computer and use it in GitHub Desktop.
Save Fulgen301/232dcac99c7c7f2fd4a9e5c0d7df7ce2 to your computer and use it in GitHub Desktop.
Destroying a secondary OpenGL context in another thread turns the window black permanently on macOS
#include <thread>
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <SDL2/SDL.h>
int main()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window *const window{SDL_CreateWindow("SDLSharedContextTest", 0, 0, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI)};
SDL_GLContext context{SDL_GL_CreateContext(window)};
SDL_GL_MakeCurrent(window, context);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
for (SDL_Event e; SDL_PollEvent(&e); );
SDL_GLContext secondary{SDL_GL_CreateContext(window)};
#define THREAD_ISSUE
#ifdef THREAD_ISSUE
std::thread{[=]
{
SDL_GL_MakeCurrent(window, secondary);
SDL_GL_MakeCurrent(window, nullptr);
SDL_GL_DeleteContext(secondary);
}}.join();
#endif
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
bool quit{false};
while (!quit)
{
for (SDL_Event e; SDL_PollEvent(&e); )
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment