SDL2 window closing immediately when created outside of DispatchQueue.main.async

First, thanks @compnerd for all your work on bringing Swift to Windows.

I tried to run code where I create a window with SDL2, which works on Linux, on Windows. Something like that:

SDL_Init(...)
...
let window = SDL_CreateWindow(...)

DispatchQueue.main.async {
  mainLoop() // update window contents every frame
}

dispatchMain()

On Linux this runs fine, the window stays open as long as the application is running. On Windows the window closes immediately even though the stuff in main.async is still running and dispatchMain() is preventing the application from exiting early.

When I put the window creation code in a main.async {} block as well, everything works as expected, the window stays open.

What is the reason for this behavior?


what was my application doing: raytracing and displaying this image (following the Raytracing in One Weekend series)

My guess would be that it is likely that the object is being discarded before the async closure executes. Windows is generally far better at ensuring that discarded items are quickly invalidated.

I don't think that this is really Windows specific.

2 Likes