Skip to content
Permalink
Browse files

Don't free the rwops if it wasn't allocated by the library!

  • Loading branch information
slouken committed Feb 26, 2008
1 parent 7a50afc commit 3e1278b0a2f7d0967be598588178b5f7c72fda9d
Showing with 26 additions and 14 deletions.
  1. +25 −14 music_mad.c
  2. +1 −0 music_mad.h
@@ -29,42 +29,53 @@
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 *
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);
}

@@ -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;

0 comments on commit 3e1278b

Please sign in to comment.