Unix: Don't send quit events during signal handler.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 08 Jun 2015 01:52:43 -0400
changeset 9720 fd60d77139fc
parent 9719 8e78db33b5de
child 9721 957a6ed39ad0
Unix: Don't send quit events during signal handler. Make note to send it, and send next time we SDL_PumpEvents(). Otherwise, we might be trying to use malloc() to push a new event on the queue while a signal is interrupting malloc() elsewhere, usually causing a crash. Fixes Bugzilla #2870.
src/events/SDL_events.c
src/events/SDL_events_c.h
src/events/SDL_quit.c
--- a/src/events/SDL_events.c	Mon Jun 08 01:17:58 2015 -0400
+++ b/src/events/SDL_events.c	Mon Jun 08 01:52:43 2015 -0400
@@ -406,6 +406,8 @@
         SDL_JoystickUpdate();
     }
 #endif
+
+    SDL_SendPendingQuit();  /* in case we had a signal handler fire, etc. */
 }
 
 /* Public functions */
--- a/src/events/SDL_events_c.h	Mon Jun 08 01:17:58 2015 -0400
+++ b/src/events/SDL_events_c.h	Mon Jun 08 01:52:43 2015 -0400
@@ -43,6 +43,8 @@
 extern int SDL_SendQuit(void);
 extern void SDL_QuitQuit(void);
 
+extern void SDL_SendPendingQuit(void);
+
 /* The event filter function */
 extern SDL_EventFilter SDL_EventOK;
 extern void *SDL_EventOKParam;
--- a/src/events/SDL_quit.c	Mon Jun 08 01:17:58 2015 -0400
+++ b/src/events/SDL_quit.c	Mon Jun 08 01:52:43 2015 -0400
@@ -20,6 +20,7 @@
 */
 #include "../SDL_internal.h"
 #include "SDL_hints.h"
+#include "SDL_assert.h"
 
 /* General quit handling code for SDL */
 
@@ -30,8 +31,8 @@
 #include "SDL_events.h"
 #include "SDL_events_c.h"
 
-
 static SDL_bool disable_signals = SDL_FALSE;
+static SDL_bool send_quit_pending = SDL_FALSE;
 
 #ifdef HAVE_SIGNAL_H
 static void
@@ -40,8 +41,9 @@
     /* Reset the signal handler */
     signal(sig, SDL_HandleSIG);
 
-    /* Signal a quit interrupt */
-    SDL_SendQuit();
+    /* Send a quit event next time the event loop pumps. */
+    /* We can't send it in signal handler; malloc() might be interrupted! */
+    send_quit_pending = SDL_TRUE;
 }
 #endif /* HAVE_SIGNAL_H */
 
@@ -136,7 +138,17 @@
 int
 SDL_SendQuit(void)
 {
+    send_quit_pending = SDL_FALSE;
     return SDL_SendAppEvent(SDL_QUIT);
 }
 
+void
+SDL_SendPendingQuit(void)
+{
+    if (send_quit_pending) {
+        SDL_SendQuit();
+        SDL_assert(!send_quit_pending);
+    }
+}
+
 /* vi: set ts=4 sw=4 expandtab: */