Destroy SDL window when finalized

SDL_DestroyWindow must be called on the main thread. If the window is
finalized, the finalizer will push a CLOSE event to the event loop
(thread-safe) and the window will be destroyed on the main thread.
This commit is contained in:
Stefanos A. 2013-11-21 09:31:32 +01:00
parent 75d4f3d07c
commit 08701d318c

View file

@ -50,6 +50,7 @@ namespace OpenTK.Platform.SDL2
bool is_focused; bool is_focused;
bool is_cursor_visible = true; bool is_cursor_visible = true;
bool exists; bool exists;
bool must_destroy;
bool disposed; bool disposed;
WindowState window_state = WindowState.Normal; WindowState window_state = WindowState.Normal;
WindowState previous_window_state = WindowState.Normal; WindowState previous_window_state = WindowState.Normal;
@ -238,7 +239,7 @@ namespace OpenTK.Platform.SDL2
if (!close_args.Cancel) if (!close_args.Cancel)
{ {
window.Closed(window, EventArgs.Empty); window.Closed(window, EventArgs.Empty);
//window.DestroyWindow(); window.must_destroy = true;
} }
break; break;
@ -395,9 +396,9 @@ namespace OpenTK.Platform.SDL2
{ {
lock (sync) lock (sync)
{ {
if (Exists) if (Exists && !must_destroy)
{ {
Debug.Print("SDL2 destroying window {0}", window.Handle); Debug.Print("SDL2 closing window {0}", window.Handle);
Event e = new Event(); Event e = new Event();
e.Type = EventType.WINDOWEVENT; e.Type = EventType.WINDOWEVENT;
@ -418,6 +419,10 @@ namespace OpenTK.Platform.SDL2
if (Exists) if (Exists)
{ {
SDL.PumpEvents(); SDL.PumpEvents();
if (must_destroy)
{
DestroyWindow();
}
} }
} }
} }
@ -899,6 +904,15 @@ namespace OpenTK.Platform.SDL2
else else
{ {
Debug.WriteLine("Sdl2NativeWindow leaked, did you forget to call Dispose()?"); Debug.WriteLine("Sdl2NativeWindow leaked, did you forget to call Dispose()?");
if (Exists)
{
// If the main loop is still running, send a close message.
// This will raise the Closing/Closed events and then destroy
// the window.
// Todo: it is probably not safe to raise events once the
// finalizer has run. Review this.
Close();
}
} }
disposed = true; disposed = true;
} }