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 3408

Summary: [Android] Opening Closing and Re-opening the same joystick fails
Product: SDL Reporter: Sylvain <sylvain.becker>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: philipp.wiesemann
Version: 2.0.4   
Hardware: All   
OS: Android (All)   

Description Sylvain 2016-08-15 21:47:36 UTC
On Android, doing open/close/open for the same joystick fails with error "failed: Joystick already opened".
Comment 1 Sylvain 2016-08-16 13:34:30 UTC
step to reproduce the isse:


case SDL_JOYDEVICEADDED:
  int which = evt.jdevice.which;

  SDL_Joystick *joystick;

  // Open: ok
  joystick = SDL_JoystickOpen(which);
  if (joystick == NULL) {
    return;
  }    
   
  // Close: ok
  SDL_JoystickClose(joystick);

  // Re-open: fails on Android with error "Joystick already opened".
  joystick = SDL_JoystickOpen(which);
  if (joystick == NULL) {
    return;
  }
Comment 2 Philipp Wiesemann 2016-08-16 20:59:13 UTC
I can not test this now but maybe it can be fixed by changing SDL_SYS_JoystickClose() in "src/joystick/android/SDL_sysjoystick.c" to:

void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
    SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
    item->joystick = NULL;
}
Comment 3 Sylvain 2016-08-16 21:19:52 UTC
Thanks! I have tested the patch and it fixes the issue.
Comment 4 Philipp Wiesemann 2016-08-17 19:28:00 UTC
This bug was fixed here: https://hg.libsdl.org/SDL/rev/3501018c6df2

And it was fixed for Emscripten here: https://hg.libsdl.org/SDL/rev/ba7dc3ebb81e

Thanks!
Comment 5 Sylvain 2016-08-18 14:18:21 UTC
Thanks for the fast help! By the way, you fixed it:) I just reported it !
Comment 6 Sylvain 2016-08-25 10:49:03 UTC
It seems to work for most of the cases, but I got this stack trace on Android Developer Console. Only one, after thousands of updates.

According to the code, that happened after receiving a "SDL_JOYDEVICEREMOVED".

... I would add a safe check :)

  SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
+ if (item) {
    item->joystick = NULL
+ }


*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'samsung/hltexx/hlte:5.0/LRX21V/N9005XXUGBOK6:user/release-keys'
Revision: '8'
ABI: 'arm'
pid: 26152, tid: 26216, name: SDLThread >>> net.jeu.myapp <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x20
r0 00000000 r1 00000000 r2 00000003 r3 b6edc004
r4 937fce40 r5 951c5270 r6 00000000 r7 951c4090
r8 00000000 r9 ffffffff sl 951c4098 fp 00000000
ip b46a5a30 sp 951c4088 lr b464b715 pc b464c5c0 cpsr 40070030

backtrace:
#00 pc 000485c0 /data/app/net.jeu.myapp-1/lib/arm/libSDL2.so (SDL_SYS_JoystickClose+3)
#01 pc 00047711 /data/app/net.jeu.myapp-1/lib/arm/libSDL2.so (SDL_JoystickClose_REAL+28)
#03 pc 0006bca3 /data/app/net.jeu.myapp-1/lib/arm/libmain.so (pollEvent(int*)+826)
Comment 7 Philipp Wiesemann 2016-08-25 20:39:23 UTC
This patch is now https://hg.libsdl.org/SDL/rev/74c39dbe8d30 (and https://hg.libsdl.org/SDL/rev/d1c27f616999), thanks!