Permalink
Browse files

Protect against recursion in Closing event

Misbehaving clients that shall not be named here may call
GameWindow.Close() inside the GameWindow.Closing event. This causes
recursion in SDL2, crashing the application.

This patch adds a guard to protect against recursion when calling
GameWindow.Close().
  • Loading branch information...
1 parent 39a216f commit 1392d48ec002d16ae915369dcf714273c438068d @thefiddler thefiddler committed Dec 16, 2013
Showing with 12 additions and 2 deletions.
  1. +12 −2 Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
@@ -53,6 +53,7 @@ class Sdl2NativeWindow : INativeWindow, IInputDriver
bool exists;
bool must_destroy;
bool disposed;
+ volatile bool is_in_closing_event;
WindowState window_state = WindowState.Normal;
WindowState previous_window_state = WindowState.Normal;
WindowBorder window_border = WindowBorder.Resizable;
@@ -303,7 +304,16 @@ static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e)
{
case WindowEventID.CLOSE:
var close_args = new System.ComponentModel.CancelEventArgs();
- window.Closing(window, close_args);
+ try
+ {
+ window.is_in_closing_event = true;
+ window.Closing(window, close_args);
+ }
+ finally
+ {
+ window.is_in_closing_event = false;
+ }
+
if (!close_args.Cancel)
{
window.Closed(window, EventArgs.Empty);
@@ -463,7 +473,7 @@ public void Close()
{
lock (sync)
{
- if (Exists && !must_destroy)
+ if (Exists && !must_destroy && !is_in_closing_event)
{
Debug.Print("SDL2 closing window {0}", window.Handle);

0 comments on commit 1392d48

Please sign in to comment.