Mysterious Mix_OpenAudio() seg fault

I get a seg fault in Mix_OpenAudio() in the following code
when the sound device is not available. The segfault only
appears if the code is compiled in c++, that is with

g++ mixer_test.c sdl-config --cflags --libs -lSDL_mixer

instead of

gcc mixer_test.c sdl-config --cflags --libs -lSDL_mixer

Also, the SDL_EnableKeyRepeat() call is necessary, without
it there’s no seg fault.

I’m using version 1.2.4 of SDL and SDL_mixer, the current
version in Debian testing. My sound card is a Gravis
Ultrasound, if that matters.

Here’s the code:

#ifdef __cplusplus
#include
#else
#include <stdio.h>
#endif
#include <SDL.h>
#include <SDL_mixer.h>

void error_out(const char* msg)
{
const char* err = SDL_GetError();

#ifdef __cplusplus
std::cerr << msg << ": " << err << std::endl;
#else
write(2, msg, strlen(msg));
write(2, ": ", 2);
write(2, err, strlen(err));
write(2, “\n”, 1);
#endif
}

int main()
{
/* have to initialize video too to keep SDL happy */
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
error_out(“Couldn’t initialize SDL”);
exit(0);
}

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

/* Open the audio device */
if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
error_out(“Couldn’t initialize sound”);
else if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 2048) < 0)
error_out(“Couldn’t open audio device”);
else
Mix_CloseAudio();

SDL_Quit();

return 0;
}

and the backtrace:

#0 0x40006eed in do_lookup () from /lib/ld-linux.so.2
#1 0x4000606b in _dl_lookup_symbol_internal () from /lib/ld-linux.so.2
#2 0x40007709 in elf_machine_rel.0 () from /lib/ld-linux.so.2
#3 0x40007bdd in elf_dynamic_do_rel.7 () from /lib/ld-linux.so.2
#4 0x40007fbd in _dl_relocate_object_internal () from /lib/ld-linux.so.2
#5 0x40283108 in getutmpx () from /lib/libc.so.6
#6 0x400095be in _dl_catch_error_internal () from /lib/ld-linux.so.2
#7 0x402833d6 in _dl_open () from /lib/libc.so.6
#8 0x4049ee79 in __curses_usegpm () from /lib/libdl.so.2
#9 0x400095be in _dl_catch_error_internal () from /lib/ld-linux.so.2
#10 0x4049f27d in dlerror () from /lib/libdl.so.2
#11 0x4049eeb7 in dlopen () from /lib/libdl.so.2
#12 0x40331c0f in lt_dlseterror () from /usr/lib/libartsc.so.0
#13 0x40332422 in lt_dlexit () from /usr/lib/libartsc.so.0
#14 0x40332546 in lt_dlexit () from /usr/lib/libartsc.so.0
#15 0x4033359f in lt_dlopen () from /usr/lib/libartsc.so.0
#16 0x40331474 in _init () from /usr/lib/libartsc.so.0
#17 0x403316a2 in arts_init () from /usr/lib/libartsc.so.0
#18 0x40035a72 in SDL_FreeWAV () from /usr/lib/libSDL-1.2.so.0
#19 0x400315f0 in SDL_OpenAudio () from /usr/lib/libSDL-1.2.so.0
#20 0x400fd85a in Mix_OpenAudio () from /usr/lib/libSDL_mixer-1.2.so.0
#21 0x08048916 in main ()
#22 0x401aea51 in __libc_start_main () from /lib/libc.so.6

Any idea what’s going on here?

Ron Steinke

“The sound of gunfire, off in the distance. I’m getting used to it now.”
– Talking Heads