From 47abe4e3b37e14191321408ea791f693ca1c982c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 9 Jan 2020 14:53:30 -0800 Subject: [PATCH] Fixed crash when unplugging a HIDAPI controller --- src/joystick/hidapi/SDL_hidapijoystick.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c index b9277f925..925982fda 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick.c +++ b/src/joystick/hidapi/SDL_hidapijoystick.c @@ -491,10 +491,6 @@ HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device) /* Disconnect any joysticks */ while (device->num_joysticks) { - SDL_Joystick *joystick = SDL_JoystickFromInstanceID(device->joysticks[0]); - if (joystick) { - HIDAPI_JoystickClose(joystick); - } HIDAPI_JoystickDisconnected(device, device->joysticks[0]); } @@ -606,6 +602,11 @@ HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID for (i = 0; i < device->num_joysticks; ++i) { if (device->joysticks[i] == joystickID) { + SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystickID); + if (joystick) { + HIDAPI_JoystickClose(joystick); + } + SDL_memcpy(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1); --device->num_joysticks; --SDL_HIDAPI_numjoysticks;