Skip to content

Commit

Permalink
Don't free the rwops if it wasn't allocated by the library!
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 26, 2008
1 parent 7a50afc commit 3e1278b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 25 additions & 14 deletions music_mad.c
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions music_mad.h
Expand Up @@ -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;
Expand Down

0 comments on commit 3e1278b

Please sign in to comment.