Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 08701d318c3b2be4063d84a1e962b1e92b7d5c7a 1 parent 75d4f3d
@thefiddler thefiddler authored
Showing with 17 additions and 3 deletions.
  1. +17 −3 Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
View
20 Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
@@ -50,6 +50,7 @@ class Sdl2NativeWindow : INativeWindow, IInputDriver
bool is_focused;
bool is_cursor_visible = true;
bool exists;
+ bool must_destroy;
bool disposed;
WindowState window_state = WindowState.Normal;
WindowState previous_window_state = WindowState.Normal;
@@ -238,7 +239,7 @@ static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e)
if (!close_args.Cancel)
{
window.Closed(window, EventArgs.Empty);
- //window.DestroyWindow();
+ window.must_destroy = true;
}
break;
@@ -395,9 +396,9 @@ public void Close()
{
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();
e.Type = EventType.WINDOWEVENT;
@@ -418,6 +419,10 @@ public void ProcessEvents()
if (Exists)
{
SDL.PumpEvents();
+ if (must_destroy)
+ {
+ DestroyWindow();
+ }
}
}
}
@@ -899,6 +904,15 @@ void Dispose(bool manual)
else
{
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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.