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

Summary: Crash disconnecting gamepad on macOS Catalina 10.15
Product: SDL Reporter: Dyami Caliri <dyami>
Component: joystickAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: blocker    
Priority: P2 CC: cameron.gutman
Version: 2.0.10Keywords: target-2.0.12
Hardware: x86   
OS: Other   
Attachments: Switch to IOHIDManagerRegisterDeviceRemovalCallback

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