Skip to content

Commit

Permalink
Fixed crash when unplugging a HIDAPI controller
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 9, 2020
1 parent f34795d commit 47abe4e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/joystick/hidapi/SDL_hidapijoystick.c
Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 47abe4e

Please sign in to comment.