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 2767

Summary: SDL_JoystickInstanceID returns -1 while handling Joystick disconnect event
Product: SDL Reporter: JKaniarz <john>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: philipp.wiesemann
Version: 2.0.3   
Hardware: All   
OS: All   

Description JKaniarz 2014-10-26 01:16:14 UTC
When handling the SDL_JoyDeviceEvent for SDL_JOYDEVICEREMOVED there is no way to match the "which" field to a SDL_Joystick * because SDL_JostickInstanceID() return -1. This requires maintaining a map of SDL_JoystickID to SDL_Joystick* on the user side. 

At a glance, modifying SDL_JoystickInstanceID() to not call SDL_PrivateJoystickValid() would allow the ID to perpetuate after the joystick is closed. As IDs are never reused this shouldn't cause any trouble.

Here is an implementation of my proposal.

SDL_JoystickID
SDL_JoystickInstanceID(SDL_Joystick * joystick)
{
    if (!joystick) {
        return (-1);
    }

    return (joystick->instance_id);
}
Comment 1 Sam Lantinga 2017-08-14 04:45:27 UTC
All SDL_PrivateJoystickValid() does is check to see if the joystick is NULL.

Can you provide a test case that we can use to see what you mean, and a patch that you've tested that fixes the problem?

Thanks!
Comment 2 JKaniarz 2017-08-15 00:23:29 UTC
The commit "2015-03-24	Cleanups in the joystick code." removed these lines from SDL_PrivateJoystickValid():

    if ( joystick && joystick->closed )
    {
        valid = 0;
    }


I believe they were causing the issue I was having where the pointer was still good but I was unable to use SDL_JoystickInstanceID() during a SDL_JOYDEVICEREMOVED event. I no longer have the code that was breaking so I'm unable to test but I suspect it was inadvertently fixed.
Comment 3 Sam Lantinga 2017-08-15 01:20:02 UTC
Oh, okay, thanks!