The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_PollEvent() crash when unplugging USB gamepad in OS X
Scam


Joined: 14 May 2015
Posts: 1
I am trying to implement a gamepad support to a cross-platform game. The game itself does not utilise SDL but I decided to create the gamepad support with it because version 2.0 promises to support hot plugging. The Windows version of the game works correctly and the gamepad plugging and unplugging events are recognised like they should be. Some problems appeared with OS X.

In OS X the game uses OpenGL and the game loop is run in CVDisplayLink thread. My first problem was that the SDL never recognised USB plugging or unplugging during the game (it only registered gamepads at startup). The obvious reason for this was that the SDL_PollEvent() should be called from main thread. So I changed my gamepad handling function call
Code:
 GamePadHandler::getInstance().handleGamePad()
to
Code:
dispatch_async(dispatch_get_main_queue(),^ { GamePadHandler::getInstance().handleGamePad();});


With this modification the game works in debug mode without any problems. However, when I compile the game with release settings, unplugging the gamepad causes SDL_PollEvent() to crash with EXC_BAD_ACCESS (code=1, address=0x40dedeadbec0). The stack trace shows that the SDL_PollEvent() goes ultimately to FreeDevice() function in SDL_sysjoystick.c, where IOHIDDeviceUnscheduleFromRunLoop() function is called and then the game crashes.

The next obvious step for me is of course to create a minimal running example of this problem, which I am going to do next. I am not very proficient with OS X so it may take time. That's why I wanted to ask some clarifying questions about this issue:

1) Has someone experienced anything like this before and if, how you handled it?
2) Is it ok to call SDL_PollEvent with dispatch_async()? If not, how the gamepad support should be implemented in a case where the game loop is not run in the main thread?