Don't free the rwops if it wasn't allocated by the library!
authorSam Lantinga <slouken@libsdl.org>
Tue, 26 Feb 2008 11:44:20 +0000
changeset 380161aa45d00c2
parent 379 c300eee1d1e4
child 381 2064088ea781
Don't free the rwops if it wasn't allocated by the library!
music_mad.c
music_mad.h
     1.1 --- a/music_mad.c	Mon Feb 04 17:10:08 2008 +0000
     1.2 +++ b/music_mad.c	Tue Feb 26 11:44:20 2008 +0000
     1.3 @@ -29,13 +29,20 @@
     1.4  mad_data *
     1.5  mad_openFile(const char *filename, SDL_AudioSpec *mixer) {
     1.6    SDL_RWops *rw;
     1.7 +  mad_data *mp3_mad;
     1.8  
     1.9    rw = SDL_RWFromFile(filename, "rb");
    1.10    if (rw == NULL) {
    1.11  	return NULL;
    1.12    }
    1.13  
    1.14 -  return mad_openFileRW(rw, mixer);
    1.15 +  mp3_mad = mad_openFileRW(rw, mixer);
    1.16 +  if (mp3_mad == NULL) {
    1.17 +	SDL_FreeRW(rw);
    1.18 +	return NULL;
    1.19 +  }
    1.20 +  mp3_mad->freerw = SDL_TRUE;
    1.21 +  return data;
    1.22  }
    1.23  
    1.24  mad_data *
    1.25 @@ -43,28 +50,32 @@
    1.26    mad_data *mp3_mad;
    1.27  
    1.28    mp3_mad = (mad_data *)malloc(sizeof(mad_data));
    1.29 -  mp3_mad->rw = rw;
    1.30 -  mad_stream_init(&mp3_mad->stream);
    1.31 -  mad_frame_init(&mp3_mad->frame);
    1.32 -  mad_synth_init(&mp3_mad->synth);
    1.33 -  mp3_mad->frames_read = 0;
    1.34 -  mad_timer_reset(&mp3_mad->next_frame_start);
    1.35 -  mp3_mad->volume = MIX_MAX_VOLUME;
    1.36 -  mp3_mad->status = 0;
    1.37 -  mp3_mad->output_begin = 0;
    1.38 -  mp3_mad->output_end = 0;
    1.39 -  mp3_mad->mixer = *mixer;
    1.40 -
    1.41 +  if (mp3_mad) {
    1.42 +	mp3_mad->rw = rw;
    1.43 +	mp3_mad->freerw = SDL_FALSE;
    1.44 +	mad_stream_init(&mp3_mad->stream);
    1.45 +	mad_frame_init(&mp3_mad->frame);
    1.46 +	mad_synth_init(&mp3_mad->synth);
    1.47 +	mp3_mad->frames_read = 0;
    1.48 +	mad_timer_reset(&mp3_mad->next_frame_start);
    1.49 +	mp3_mad->volume = MIX_MAX_VOLUME;
    1.50 +	mp3_mad->status = 0;
    1.51 +	mp3_mad->output_begin = 0;
    1.52 +	mp3_mad->output_end = 0;
    1.53 +	mp3_mad->mixer = *mixer;
    1.54 +  }
    1.55    return mp3_mad;
    1.56  }
    1.57  
    1.58  void
    1.59  mad_closeFile(mad_data *mp3_mad) {
    1.60 -  SDL_FreeRW(mp3_mad->rw);
    1.61    mad_stream_finish(&mp3_mad->stream);
    1.62    mad_frame_finish(&mp3_mad->frame);
    1.63    mad_synth_finish(&mp3_mad->synth);
    1.64  
    1.65 +  if (mp3_mad->freerw) {
    1.66 +	SDL_FreeRW(mp3_mad->rw);
    1.67 +  }
    1.68    free(mp3_mad);
    1.69  }
    1.70  
     2.1 --- a/music_mad.h	Mon Feb 04 17:10:08 2008 +0000
     2.2 +++ b/music_mad.h	Tue Feb 26 11:44:20 2008 +0000
     2.3 @@ -43,6 +43,7 @@
     2.4  
     2.5  typedef struct {
     2.6    SDL_RWops *rw;
     2.7 +  SDL_bool freerw;
     2.8    struct mad_stream stream;
     2.9    struct mad_frame frame;
    2.10    struct mad_synth synth;