Windows: Fixed crash if quitting without closing an XInput haptic device.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 02 Mar 2014 00:02:56 -0500
changeset 8281 f280c6b5d08c
parent 8280 860092ac58d6
child 8282 484d2404c472
Windows: Fixed crash if quitting without closing an XInput haptic device.
src/haptic/windows/SDL_syshaptic.c
--- a/src/haptic/windows/SDL_syshaptic.c	Sat Mar 01 22:27:21 2014 -0500
+++ b/src/haptic/windows/SDL_syshaptic.c	Sun Mar 02 00:02:56 2014 -0500
@@ -939,20 +939,32 @@
 {
     SDL_hapticlist_item *item;
     SDL_hapticlist_item *next = NULL;
+    SDL_Haptic *hapticitem = NULL;
 
-    if (loaded_xinput) {
-        WIN_UnloadXInputDLL();
-        loaded_xinput = SDL_FALSE;
+    extern SDL_Haptic *SDL_haptics;
+    for (hapticitem = SDL_haptics; hapticitem; hapticitem = hapticitem->next) {
+        if ((hapticitem->hwdata->bXInputHaptic) && (hapticitem->hwdata->thread)) {
+            /* we _have_ to stop the thread before we free the XInput DLL! */
+            hapticitem->hwdata->stopThread = 1;
+            SDL_WaitThread(hapticitem->hwdata->thread, NULL);
+            hapticitem->hwdata->thread = NULL;
+        }
     }
 
     for (item = SDL_hapticlist; item; item = next) {
         /* Opened and not closed haptics are leaked, this is on purpose.
          * Close your haptic devices after usage. */
+        /* !!! FIXME: (...is leaking on purpose a good idea?) */
         next = item->next;
         SDL_free(item->name);
         SDL_free(item);
     }
 
+    if (loaded_xinput) {
+        WIN_UnloadXInputDLL();
+        loaded_xinput = SDL_FALSE;
+    }
+
     if (dinput != NULL) {
         IDirectInput8_Release(dinput);
         dinput = NULL;