From 542356825590c07b72cfc9094669fa9796a48fd0 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 4 Jan 2012 01:41:10 -0500 Subject: [PATCH] Fixed bug 1360 - fluidsynth backend segfaults sdl-mixer We need to initialize fluidsynth! Also cleaned up the other music initialization so they're consistent. --- SDL_mixer.h | 2 +- fluidsynth.c | 4 ++++ music_flac.c | 47 ++++++++++++++++++++++++++--------------------- music_ogg.c | 13 +++++++------ 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/SDL_mixer.h b/SDL_mixer.h index d96f48d0..9c25ef6b 100644 --- a/SDL_mixer.h +++ b/SDL_mixer.h @@ -70,7 +70,7 @@ typedef enum MIX_INIT_MOD = 0x00000002, MIX_INIT_MP3 = 0x00000004, MIX_INIT_OGG = 0x00000008, - MIX_INIT_FLUIDSYNTH = 0x00000016 + MIX_INIT_FLUIDSYNTH = 0x00000010 } MIX_InitFlags; /* Loads dynamic libraries and prepares them for use. Flags should be diff --git a/fluidsynth.c b/fluidsynth.c index d7a72d1a..8a199570 100644 --- a/fluidsynth.c +++ b/fluidsynth.c @@ -71,6 +71,10 @@ static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynth FluidSynthMidiSong *song; fluid_settings_t *settings = NULL; + if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) { + return NULL; + } + if ((song = malloc(sizeof(FluidSynthMidiSong)))) { memset(song, 0, sizeof(FluidSynthMidiSong)); diff --git a/music_flac.c b/music_flac.c index c80900b2..e63fc23b 100644 --- a/music_flac.c +++ b/music_flac.c @@ -306,6 +306,13 @@ FLAC_music *FLAC_new_RW(SDL_RWops *rw, int freerw) int init_stage = 0; int was_error = 1; + if (!Mix_Init(MIX_INIT_FLAC)) { + if (freerw) { + SDL_RWclose(rw); + } + return NULL; + } + music = (FLAC_music *)malloc ( sizeof (*music)); if (music) { /* Initialize the music structure */ @@ -323,35 +330,33 @@ FLAC_music *FLAC_new_RW(SDL_RWops *rw, int freerw) music->flac_data.data_len = 0; music->flac_data.data_read = 0; - if (Mix_Init(MIX_INIT_FLAC)) { - init_stage++; // stage 1! + init_stage++; // stage 1! - music->flac_decoder = flac.FLAC__stream_decoder_new (); + music->flac_decoder = flac.FLAC__stream_decoder_new (); - if (music->flac_decoder != NULL) { - init_stage++; // stage 2! + if (music->flac_decoder != NULL) { + init_stage++; // stage 2! - if (flac.FLAC__stream_decoder_init_stream( - music->flac_decoder, - flac_read_music_cb, flac_seek_music_cb, - flac_tell_music_cb, flac_length_music_cb, - flac_eof_music_cb, flac_write_music_cb, - flac_metadata_music_cb, flac_error_music_cb, - music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) { - init_stage++; // stage 3! + if (flac.FLAC__stream_decoder_init_stream( + music->flac_decoder, + flac_read_music_cb, flac_seek_music_cb, + flac_tell_music_cb, flac_length_music_cb, + flac_eof_music_cb, flac_write_music_cb, + flac_metadata_music_cb, flac_error_music_cb, + music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) { + init_stage++; // stage 3! - if (flac.FLAC__stream_decoder_process_until_end_of_metadata - (music->flac_decoder)) { - was_error = 0; - } else { - SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed"); - } + if (flac.FLAC__stream_decoder_process_until_end_of_metadata + (music->flac_decoder)) { + was_error = 0; } else { - SDL_SetError("FLAC__stream_decoder_init_stream() failed"); + SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed"); } } else { - SDL_SetError("FLAC__stream_decoder_new() failed"); + SDL_SetError("FLAC__stream_decoder_init_stream() failed"); } + } else { + SDL_SetError("FLAC__stream_decoder_new() failed"); } if (was_error) { diff --git a/music_ogg.c b/music_ogg.c index 3fbba04c..5a3a2971 100644 --- a/music_ogg.c +++ b/music_ogg.c @@ -72,6 +72,13 @@ OGG_music *OGG_new_RW(SDL_RWops *rw, int freerw) OGG_music *music; ov_callbacks callbacks; + if ( !Mix_Init(MIX_INIT_OGG) ) { + if ( freerw ) { + SDL_RWclose(rw); + } + return(NULL); + } + SDL_memset(&callbacks, 0, sizeof(callbacks)); callbacks.read_func = sdl_read_func; callbacks.seek_func = sdl_seek_func; @@ -87,12 +94,6 @@ OGG_music *OGG_new_RW(SDL_RWops *rw, int freerw) OGG_setvolume(music, MIX_MAX_VOLUME); music->section = -1; - if ( !Mix_Init(MIX_INIT_OGG) ) { - if ( freerw ) { - SDL_RWclose(rw); - } - return(NULL); - } if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) { free(music); if ( freerw ) {