Fixed Bug 5034 - Replugging in a controller crashes on macOS in SDL 2.0.12
authorDavid Ludwig <dludwig@pobox.com>
Mon, 16 Mar 2020 19:24:25 -0400
changeset 13638ca87b62e4e17
parent 13637 118aed2f33e6
child 13639 112bdb9abf2c
Fixed Bug 5034 - Replugging in a controller crashes on macOS in SDL 2.0.12

The Darwin/macOS joystick driver was freeing its joystick's hwdata field
without zeroing it out in any live instance of SDL_Joystick.
src/joystick/darwin/SDL_sysjoystick.c
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Mon Mar 16 14:49:20 2020 -0700
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Mon Mar 16 19:24:25 2020 -0400
     1.3 @@ -125,6 +125,7 @@
     1.4  FreeDevice(recDevice *removeDevice)
     1.5  {
     1.6      recDevice *pDeviceNext = NULL;
     1.7 +    SDL_Joystick *joystick = NULL;
     1.8      if (removeDevice) {
     1.9          if (removeDevice->deviceRef) {
    1.10              IOHIDDeviceUnscheduleFromRunLoop(removeDevice->deviceRef, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
    1.11 @@ -146,6 +147,14 @@
    1.12          }
    1.13          removeDevice->pNext = NULL;
    1.14  
    1.15 +        /* clear out any reference to this recDevice that are being
    1.16 +         * held by a live instance of SDL_Joystick
    1.17 +         */
    1.18 +        joystick = SDL_JoystickFromInstanceID(removeDevice->instance_id);
    1.19 +        if (joystick) {
    1.20 +            joystick->hwdata = NULL;
    1.21 +        }
    1.22 +
    1.23          /* free element lists */
    1.24          FreeElementList(removeDevice->firstAxis);
    1.25          FreeElementList(removeDevice->firstButton);
    1.26 @@ -871,6 +880,10 @@
    1.27      /* Scale and average the two rumble strengths */
    1.28      Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
    1.29  
    1.30 +    if (!device) {
    1.31 +        return SDL_SetError("Rumble failed, device disconnected");
    1.32 +    }
    1.33 +
    1.34      if (!device->ffservice) {
    1.35          return SDL_Unsupported();
    1.36      }