We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4888 - Crash disconnecting gamepad on macOS Catalina 10.15
Summary: Crash disconnecting gamepad on macOS Catalina 10.15
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: joystick (show other bugs)
Version: 2.0.10
Hardware: x86 Other
: P2 blocker
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords: target-2.0.12
Depends on:
Blocks:
 
Reported: 2019-12-06 19:27 UTC by Dyami Caliri
Modified: 2019-12-22 21:54 UTC (History)
1 user (show)

See Also:


Attachments
Switch to IOHIDManagerRegisterDeviceRemovalCallback (3.82 KB, patch)
2019-12-21 20:47 UTC, Cameron Gutman
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dyami Caliri 2019-12-06 19:27:56 UTC
macOS Catalina appears to not call the 'IOHIDDeviceRegisterRemovalCallback' callback.
https://forums.developer.apple.com/thread/124444

When you unplug a joystick/gamepad, SDL does not receive a notification, and then subsequently crashes when trying to use the IOHIDDeviceRef.

I was able to work around this by registering for a device removal callback on the manager, which still gets called.

In darwin/SDL_sysjoystick.c, I added a device removal callback:

In 'ConfigHIDManager' function:
IOHIDManagerRegisterDeviceRemovalCallback(hidman, JoystickManagerDeviceWasRemovedCallback, NULL);

Then, I added the callback, like this:
static void
JoystickManagerDeviceWasRemovedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
{
    recDevice *curdevice = gpDeviceList;
    while ( curdevice )
    {
        if (curdevice->deviceRef == ioHIDDeviceObject)
        {
            IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, NULL, NULL);
            JoystickDeviceWasRemovedCallback(curdevice, res, sender);
            break;
        }
        curdevice = curdevice->pNext;
    }
}

The code works on both Catalina and older systems.
Comment 1 Cameron Gutman 2019-12-21 20:47:35 UTC
Created attachment 4134 [details]
Switch to IOHIDManagerRegisterDeviceRemovalCallback

This patch fixes the issue for me.
Comment 2 Sam Lantinga 2019-12-22 21:54:54 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/d7e699bc1173