From 3e1278b0a2f7d0967be598588178b5f7c72fda9d Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 26 Feb 2008 11:44:20 +0000 Subject: [PATCH] Don't free the rwops if it wasn't allocated by the library! --- music_mad.c | 39 +++++++++++++++++++++++++-------------- music_mad.h | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/music_mad.c b/music_mad.c index 106315e3..edd751a0 100644 --- a/music_mad.c +++ b/music_mad.c @@ -29,13 +29,20 @@ mad_data * mad_openFile(const char *filename, SDL_AudioSpec *mixer) { SDL_RWops *rw; + mad_data *mp3_mad; rw = SDL_RWFromFile(filename, "rb"); if (rw == NULL) { return NULL; } - return mad_openFileRW(rw, mixer); + mp3_mad = mad_openFileRW(rw, mixer); + if (mp3_mad == NULL) { + SDL_FreeRW(rw); + return NULL; + } + mp3_mad->freerw = SDL_TRUE; + return data; } mad_data * @@ -43,28 +50,32 @@ mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer) { mad_data *mp3_mad; mp3_mad = (mad_data *)malloc(sizeof(mad_data)); - mp3_mad->rw = rw; - mad_stream_init(&mp3_mad->stream); - mad_frame_init(&mp3_mad->frame); - mad_synth_init(&mp3_mad->synth); - mp3_mad->frames_read = 0; - mad_timer_reset(&mp3_mad->next_frame_start); - mp3_mad->volume = MIX_MAX_VOLUME; - mp3_mad->status = 0; - mp3_mad->output_begin = 0; - mp3_mad->output_end = 0; - mp3_mad->mixer = *mixer; - + if (mp3_mad) { + mp3_mad->rw = rw; + mp3_mad->freerw = SDL_FALSE; + mad_stream_init(&mp3_mad->stream); + mad_frame_init(&mp3_mad->frame); + mad_synth_init(&mp3_mad->synth); + mp3_mad->frames_read = 0; + mad_timer_reset(&mp3_mad->next_frame_start); + mp3_mad->volume = MIX_MAX_VOLUME; + mp3_mad->status = 0; + mp3_mad->output_begin = 0; + mp3_mad->output_end = 0; + mp3_mad->mixer = *mixer; + } return mp3_mad; } void mad_closeFile(mad_data *mp3_mad) { - SDL_FreeRW(mp3_mad->rw); mad_stream_finish(&mp3_mad->stream); mad_frame_finish(&mp3_mad->frame); mad_synth_finish(&mp3_mad->synth); + if (mp3_mad->freerw) { + SDL_FreeRW(mp3_mad->rw); + } free(mp3_mad); } diff --git a/music_mad.h b/music_mad.h index 2556862e..8445f03f 100644 --- a/music_mad.h +++ b/music_mad.h @@ -43,6 +43,7 @@ enum { typedef struct { SDL_RWops *rw; + SDL_bool freerw; struct mad_stream stream; struct mad_frame frame; struct mad_synth synth;