Fixed bug 1238 - SDL_SetKeyboardFocus may send events to already destroyed windows
authorSam Lantinga <slouken@libsdl.org>
Sat, 14 Jan 2012 01:38:11 -0500
changeset 6214 b1a71f8189a6
parent 6213 e50324ad38ee
child 6216 c6ce4c89fcea
Fixed bug 1238 - SDL_SetKeyboardFocus may send events to already destroyed windows bastien.bouclet@gmail.com 2011-06-26 02:15:36 PDT SDL_Keyboard::focus does not seem to be reset when the window that has focus is destroyed, resulting in the following crash : #0 X11_SetWindowGammaRamp (_this=0x8ed9cb0, window=0x91f25c0, ramp=0x0) at src/video/x11/SDL_x11window.c:948 #1 0x001bd15e in SDL_OnWindowFocusLost (window=0x91f25c0) at src/video/SDL_video.c:1900 #2 0x00168a2e in SDL_SendWindowEvent (window=0x91f25c0, windowevent=<value optimized out>, data1=0, data2=0) at src/events/SDL_windowevents.c:157 #3 0x00166454 in SDL_SetKeyboardFocus (window=0x9678a08) at src/events/SDL_keyboard.c:612
src/video/SDL_video.c
--- a/src/video/SDL_video.c	Sat Jan 14 00:49:25 2012 -0500
+++ b/src/video/SDL_video.c	Sat Jan 14 01:38:11 2012 -0500
@@ -1932,6 +1932,17 @@
 
     CHECK_WINDOW_MAGIC(window, );
 
+    /* Restore video mode, etc. */
+    SDL_HideWindow(window);
+
+    /* Make sure this window no longer has focus */
+    if (SDL_GetKeyboardFocus() == window) {
+        SDL_SetKeyboardFocus(NULL);
+    }
+    if (SDL_GetMouseFocus() == window) {
+        SDL_SetMouseFocus(NULL);
+    }
+
     /* make no context current if this is the current context window. */
     if (window->flags & SDL_WINDOW_OPENGL) {
         if (_this->current_glwin == window) {
@@ -1939,9 +1950,6 @@
         }
     }
 
-    /* Restore video mode, etc. */
-    SDL_HideWindow(window);
-
     if (window->surface) {
         window->surface->flags &= ~SDL_DONTFREE;
         SDL_FreeSurface(window->surface);