Joystick Segfault

In a little game I was writing, I started to add joystick support, and found
that I am getting a segfault when calling SDL_JoystickUpdate() in my main
loop.

The testjoystick program reports that there are 0 joysticks detected, which
is probably the source of the error. My game checks the number of joysticks
available, and only initialize those that are available and that it will use,
which is up to four right now. So my game must therefore be initializing no
joysticks, but the main loop code is still calling SDL_JoystickUpdate(), and
getting the crash.

Shouldn’t SDL_JoystickUpdate() gracefully handle the case of no joysticks?

-Ray

Shouldn’t SDL_JoystickUpdate() gracefully handle the case of no joysticks?

It does, it sounds like you have an old version of SDL.
Try grabbing the latest CVS and try that:
http://www.libsdl.org/cvs.html

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

Actually, I am using the CVS code, but I haven’t done an update in about a
week :slight_smile:

I found my problem. I was not initializing the joystick subsystem of SDL. I
still think it should be able to handle that without segfaulting, but since
it’s technically “undefined behavior” I can’t complain :slight_smile:

It should, however, probably be noted in the SDL_JoystickOpen() and
SDL_NumJoysticks() calls that they will not function correctly unless the
joystick subsection was initialized in the call to SDL_Init(). I wasn’t aware
that the joystick parts needed explicit initialization, since it wasn’t
referenced.

I was able to get everything working nicely, although I found that Mandrake
8.0 didn’t correctly set up the joystick driver - it needed a little manual
intervention.

-RayOn Sunday 17 June 2001 19:10, you wrote:

Shouldn’t SDL_JoystickUpdate() gracefully handle the case of no
joysticks?

It does, it sounds like you have an old version of SDL.
Try grabbing the latest CVS and try that:
http://www.libsdl.org/cvs.html

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.