Segfault when trying to close joystick

Hi,

I’m running into a bug when I try to close a joystick.

In SDL_sysjoystick.c (around line 984) it crashes just after the close( joystick->hwdata->fd ), when the function returns from this, joystick->hwdata is reset to a NULL pointer, and the following if statement crashes.

I’m not sure why this happens…

sincerely,
Marije

void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
if (joystick->hwdata) {
close(joystick->hwdata->fd);
if (joystick->hwdata->item) {
joystick->hwdata->item->hwdata = NULL;
}

If joystick->hwdata is NULL, then you can’t access joystick->hwdata->item
at all.On Sun, Mar 10, 2013 at 12:01 PM, nescivi wrote:

**
Hi,

I’m running into a bug when I try to close a joystick.

In SDL_sysjoystick.c (around line 984) it crashes just after the close(
joystick->hwdata->fd ), when the function returns from this,
joystick->hwdata is reset to a NULL pointer, and the following if statement
crashes.

I’m not sure why this happens…

sincerely,
Marije

void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
if (joystick->hwdata) {
close(joystick->hwdata->fd);
if (joystick->hwdata->item) {
joystick->hwdata->item->hwdata = NULL;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The problem is that close(joystick->hwdata->fd) shouldn’t result in
joystick->hwdata becoming NULL, and if it reaches that line it
obviously isn’t NULL because of the if right before that.

2013/3/11, Justin Coleman :> If joystick->hwdata is NULL, then you can’t access joystick->hwdata->item

at all.

On Sun, Mar 10, 2013 at 12:01 PM, nescivi wrote:

**
Hi,

I’m running into a bug when I try to close a joystick.

In SDL_sysjoystick.c (around line 984) it crashes just after the close(
joystick->hwdata->fd ), when the function returns from this,
joystick->hwdata is reset to a NULL pointer, and the following if
statement
crashes.

I’m not sure why this happens…

sincerely,
Marije

void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
if (joystick->hwdata) {
close(joystick->hwdata->fd);
if (joystick->hwdata->item) {
joystick->hwdata->item->hwdata = NULL;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

In SDL_sysjoystick.c (around line 984) it crashes just after the close(
joystick->hwdata->fd ), when the function returns from this,
joystick->hwdata is reset to a NULL pointer, and the following if
statement crashes.

Are you closing the joystick (or quitting the subsystem) from two
threads at once?

–ryan.

I will check what is going on with the threads.
I am creating a separate thread to handle the events, compared to where I close the joystick, so possibly this is what is going wrong.

The simplest example where I get the crash is here:


where the closing joystick is called from the osc/udp-thread.

I’m quite sure I’m not closing the joystick from both threads at once though.