Fixed memory crash loading Ogg Vorbis files on Windows
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 03:15:19 -0500
changeset 56187bdb4c81c0b
parent 560 0fc8dffb86c5
child 562 4129f7c1c68f
Fixed memory crash loading Ogg Vorbis files on Windows
The pointer to the audio data could come from SDL_LoadWAV_RW() or from our other loaders, and we need to use the correct free() to release the memory. So we'll just use the SDL memory functions for consistency.
This pretty much only matters on Windows where we can't guarantee a certain C runtime so we provide our own malloc() and friends.
CHANGES
effect_position.c
effects_internal.c
fluidsynth.c
load_aiff.c
load_flac.c
load_ogg.c
load_voc.c
mixer.c
music.c
music_cmd.c
music_flac.c
music_mad.c
music_modplug.c
music_ogg.c
wavestream.c
     1.1 --- a/CHANGES	Fri Jan 13 02:39:41 2012 -0500
     1.2 +++ b/CHANGES	Fri Jan 13 03:15:19 2012 -0500
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.12:
     1.5 +Sam Lantinga - Fri Jan 13 03:04:27 EST 2012
     1.6 + * Fixed memory crash loading Ogg Vorbis files on Windows
     1.7  Nikos Chantziaras - 2012-01-02 17:37:36 PST
     1.8   * Added Mix_LoadMUSType_RW() so you can tell SDL_mixer what type the music is
     1.9  Sam Lantinga - Sat Dec 31 19:11:59 EST 2011
     2.1 --- a/effect_position.c	Fri Jan 13 02:39:41 2012 -0500
     2.2 +++ b/effect_position.c	Fri Jan 13 03:15:19 2012 -0500
     2.3 @@ -85,14 +85,14 @@
     2.4  {
     2.5      int i;
     2.6      for (i = 0; i < position_channels; i++) {
     2.7 -        free(pos_args_array[i]);
     2.8 +        SDL_free(pos_args_array[i]);
     2.9      }
    2.10  
    2.11      position_channels = 0;
    2.12  
    2.13 -    free(pos_args_global);
    2.14 +    SDL_free(pos_args_global);
    2.15      pos_args_global = NULL;
    2.16 -    free(pos_args_array);
    2.17 +    SDL_free(pos_args_array);
    2.18      pos_args_array = NULL;
    2.19  }
    2.20  
    2.21 @@ -102,13 +102,13 @@
    2.22  {
    2.23      if (channel < 0) {
    2.24          if (pos_args_global != NULL) {
    2.25 -            free(pos_args_global);
    2.26 +            SDL_free(pos_args_global);
    2.27              pos_args_global = NULL;
    2.28          }
    2.29      }
    2.30  
    2.31      else if (pos_args_array[channel] != NULL) {
    2.32 -        free(pos_args_array[channel]);
    2.33 +        SDL_free(pos_args_array[channel]);
    2.34          pos_args_array[channel] = NULL;
    2.35      }
    2.36  }
    2.37 @@ -1149,7 +1149,7 @@
    2.38  
    2.39      if (channel < 0) {
    2.40          if (pos_args_global == NULL) {
    2.41 -            pos_args_global = malloc(sizeof (position_args));
    2.42 +            pos_args_global = SDL_malloc(sizeof (position_args));
    2.43              if (pos_args_global == NULL) {
    2.44                  Mix_SetError("Out of memory");
    2.45                  return(NULL);
    2.46 @@ -1161,7 +1161,7 @@
    2.47      }
    2.48  
    2.49      if (channel >= position_channels) {
    2.50 -        rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
    2.51 +        rc = SDL_realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
    2.52          if (rc == NULL) {
    2.53              Mix_SetError("Out of memory");
    2.54              return(NULL);
    2.55 @@ -1174,7 +1174,7 @@
    2.56      }
    2.57  
    2.58      if (pos_args_array[channel] == NULL) {
    2.59 -        pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
    2.60 +        pos_args_array[channel] = (position_args *)SDL_malloc(sizeof(position_args));
    2.61          if (pos_args_array[channel] == NULL) {
    2.62              Mix_SetError("Out of memory");
    2.63              return(NULL);
     3.1 --- a/effects_internal.c	Fri Jan 13 02:39:41 2012 -0500
     3.2 +++ b/effects_internal.c	Fri Jan 13 03:15:19 2012 -0500
     3.3 @@ -73,7 +73,7 @@
     3.4      }
     3.5  
     3.6      if (!_Eff_volume_table) {
     3.7 -        rc = malloc(256 * 256);
     3.8 +        rc = SDL_malloc(256 * 256);
     3.9          if (rc) {
    3.10              _Eff_volume_table = (void *) rc;
    3.11              for (volume = 0; volume < 256; volume++) {
    3.12 @@ -104,7 +104,7 @@
    3.13      Sint8 *rc;
    3.14  
    3.15      if (!_Eff_volume_table) {
    3.16 -        rc = malloc(256 * 256);
    3.17 +        rc = SDL_malloc(256 * 256);
    3.18          if (rc) {
    3.19              _Eff_volume_table = (void *) rc;
    3.20              for (volume = 0; volume < 256; volume++) {
     4.1 --- a/fluidsynth.c	Fri Jan 13 02:39:41 2012 -0500
     4.2 +++ b/fluidsynth.c	Fri Jan 13 03:15:19 2012 -0500
     4.3 @@ -75,7 +75,7 @@
     4.4  		return NULL;
     4.5  	}
     4.6  
     4.7 -	if ((song = malloc(sizeof(FluidSynthMidiSong)))) {
     4.8 +	if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
     4.9  		memset(song, 0, sizeof(FluidSynthMidiSong));
    4.10  
    4.11  		if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    4.12 @@ -102,7 +102,7 @@
    4.13  		} else {
    4.14  			Mix_SetError("Failed to set up audio conversion");
    4.15  		}
    4.16 -		free(song);
    4.17 +		SDL_free(song);
    4.18  	} else {
    4.19  		Mix_SetError("Insufficient memory for song");
    4.20  	}
    4.21 @@ -121,7 +121,7 @@
    4.22  	size = SDL_RWtell(rw) - offset;
    4.23  	SDL_RWseek(rw, offset, RW_SEEK_SET);
    4.24  
    4.25 -	if ((buffer = (char*) malloc(size))) {
    4.26 +	if ((buffer = (char*) SDL_malloc(size))) {
    4.27  		if(SDL_RWread(rw, buffer, size, 1) == 1) {
    4.28  			if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
    4.29  				return 1;
    4.30 @@ -131,7 +131,7 @@
    4.31  		} else {
    4.32  			Mix_SetError("Failed to read in-memory song");
    4.33  		}
    4.34 -		free(buffer);
    4.35 +		SDL_free(buffer);
    4.36  	} else {
    4.37  		Mix_SetError("Insufficient memory for song");
    4.38  	}
    4.39 @@ -155,7 +155,7 @@
    4.40  	fluidsynth.delete_fluid_player(song->player);
    4.41  	fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
    4.42  	fluidsynth.delete_fluid_synth(song->synth);
    4.43 -	free(song);
    4.44 +	SDL_free(song);
    4.45  }
    4.46  
    4.47  void fluidsynth_start(FluidSynthMidiSong *song)
    4.48 @@ -188,7 +188,7 @@
    4.49  	void *src = dest;
    4.50  
    4.51  	if (dest_len < src_len) {
    4.52 -		if (!(src = malloc(src_len))) {
    4.53 +		if (!(src = SDL_malloc(src_len))) {
    4.54  			Mix_SetError("Insufficient memory for audio conversion");
    4.55  			return result;
    4.56  		}
    4.57 @@ -214,7 +214,7 @@
    4.58  
    4.59  finish:
    4.60  	if (src != dest)
    4.61 -		free(src);
    4.62 +		SDL_free(src);
    4.63  
    4.64  	return result;
    4.65  }
     5.1 --- a/load_aiff.c	Fri Jan 13 02:39:41 2012 -0500
     5.2 +++ b/load_aiff.c	Fri Jan 13 03:15:19 2012 -0500
     5.3 @@ -224,7 +224,7 @@
     5.4  	spec->samples = 4096;		/* Good default buffer size */
     5.5  
     5.6  	*audio_len = channels * numsamples * (samplesize / 8);
     5.7 -	*audio_buf = (Uint8 *)malloc(*audio_len);
     5.8 +	*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
     5.9  	if ( *audio_buf == NULL ) {
    5.10  		SDL_SetError("Out of memory");
    5.11  		return(NULL);
     6.1 --- a/load_flac.c	Fri Jan 13 02:39:41 2012 -0500
     6.2 +++ b/load_flac.c	Fri Jan 13 03:15:19 2012 -0500
     6.3 @@ -176,7 +176,7 @@
     6.4  	if (frame->header.number.sample_number == 0) {
     6.5  		*(data->sdl_audio_len) = data->sdl_spec->size;
     6.6  		data->sdl_audio_read = 0;
     6.7 -    	*(data->sdl_audio_buf) = malloc (*(data->sdl_audio_len));
     6.8 +    	*(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len));
     6.9  
    6.10      	if (*(data->sdl_audio_buf) == NULL) {
    6.11      		SDL_SetError
    6.12 @@ -274,7 +274,7 @@
    6.13  
    6.14  	// create the client data passing information
    6.15  	FLAC_SDL_Data* client_data;
    6.16 -	client_data = (FLAC_SDL_Data *)malloc (sizeof (FLAC_SDL_Data));
    6.17 +	client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data));
    6.18  
    6.19  	if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
    6.20  		goto done;
    6.21 @@ -313,9 +313,9 @@
    6.22  
    6.23  	was_error = 0;
    6.24  
    6.25 -    /* Don't return a buffer that isn't a multiple of samplesize */
    6.26 -    samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
    6.27 -    *audio_len &= ~(samplesize - 1);
    6.28 +	/* Don't return a buffer that isn't a multiple of samplesize */
    6.29 +	samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
    6.30 +	*audio_len &= ~(samplesize - 1);
    6.31  
    6.32  done:
    6.33  	if (was_init && decoder) {
     7.1 --- a/load_ogg.c	Fri Jan 13 02:39:41 2012 -0500
     7.2 +++ b/load_ogg.c	Fri Jan 13 03:15:19 2012 -0500
     7.3 @@ -113,7 +113,7 @@
     7.4      samples = (long)vorbis.ov_pcm_total(&vf, -1);
     7.5  
     7.6      *audio_len = spec->size = samples * spec->channels * 2;
     7.7 -    *audio_buf = malloc(*audio_len);
     7.8 +    *audio_buf = SDL_malloc(*audio_len);
     7.9      if (*audio_buf == NULL)
    7.10          goto done;
    7.11  
     8.1 --- a/load_voc.c	Fri Jan 13 02:39:41 2012 -0500
     8.2 +++ b/load_voc.c	Fri Jan 13 03:15:19 2012 -0500
     8.3 @@ -411,7 +411,7 @@
     8.4          spec->channels = v.channels;
     8.5  
     8.6      *audio_len = v.rest;
     8.7 -    *audio_buf = malloc(v.rest);
     8.8 +    *audio_buf = SDL_malloc(v.rest);
     8.9      if (*audio_buf == NULL)
    8.10          goto done;
    8.11  
    8.12 @@ -423,10 +423,10 @@
    8.13              goto done;
    8.14  
    8.15          *audio_len += v.rest;
    8.16 -        ptr = realloc(*audio_buf, *audio_len);
    8.17 +        ptr = SDL_realloc(*audio_buf, *audio_len);
    8.18          if (ptr == NULL)
    8.19          {
    8.20 -            free(*audio_buf);
    8.21 +            SDL_free(*audio_buf);
    8.22              *audio_buf = NULL;
    8.23              *audio_len = 0;
    8.24              goto done;
     9.1 --- a/mixer.c	Fri Jan 13 02:39:41 2012 -0500
     9.2 +++ b/mixer.c	Fri Jan 13 03:15:19 2012 -0500
     9.3 @@ -126,7 +126,7 @@
     9.4  
     9.5  static void add_chunk_decoder(const char *decoder)
     9.6  {
     9.7 -	void *ptr = realloc(chunk_decoders, (num_decoders + 1) * sizeof (const char **));
     9.8 +	void *ptr = SDL_realloc(chunk_decoders, (num_decoders + 1) * sizeof (const char **));
     9.9  	if (ptr == NULL) {
    9.10  		return;  /* oh well, go on without it. */
    9.11  	}
    9.12 @@ -227,7 +227,7 @@
    9.13  #endif
    9.14  #ifdef MID_MUSIC
    9.15  	if (soundfont_paths) {
    9.16 -		free(soundfont_paths);
    9.17 +		SDL_free(soundfont_paths);
    9.18  	}
    9.19  #endif
    9.20  	initialized = 0;
    9.21 @@ -263,11 +263,11 @@
    9.22  	if (e != NULL) {    /* are there any registered effects? */
    9.23  		/* if this is the postmix, we can just overwrite the original. */
    9.24  		if (!posteffect) {
    9.25 -			buf = malloc(len);
    9.26 +			buf = SDL_malloc(len);
    9.27  			if (buf == NULL) {
    9.28  				return(snd);
    9.29  			}
    9.30 -		    memcpy(buf, snd, len);
    9.31 +			memcpy(buf, snd, len);
    9.32  		}
    9.33  
    9.34  		for (; e != NULL; e = e->next) {
    9.35 @@ -277,7 +277,7 @@
    9.36  		}
    9.37  	}
    9.38  
    9.39 -	/* be sure to free() the return value if != snd ... */
    9.40 +	/* be sure to SDL_free() the return value if != snd ... */
    9.41  	return(buf);
    9.42  }
    9.43  
    9.44 @@ -344,7 +344,7 @@
    9.45  					mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable);
    9.46  					SDL_MixAudio(stream+index,mix_input,mixable,volume);
    9.47  					if (mix_input != mix_channel[i].samples)
    9.48 -						free(mix_input);
    9.49 +						SDL_free(mix_input);
    9.50  
    9.51  					mix_channel[i].samples += mixable;
    9.52  					mix_channel[i].playing -= mixable;
    9.53 @@ -368,7 +368,7 @@
    9.54  					mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining);
    9.55  					SDL_MixAudio(stream+index, mix_input, remaining, volume);
    9.56  					if (mix_input != mix_channel[i].chunk->abuf)
    9.57 -						free(mix_input);
    9.58 +						SDL_free(mix_input);
    9.59  
    9.60  					--mix_channel[i].looping;
    9.61  					mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining;
    9.62 @@ -443,7 +443,7 @@
    9.63  	}
    9.64  
    9.65  	num_channels = MIX_CHANNELS;
    9.66 -	mix_channel = (struct _Mix_Channel *) malloc(num_channels * sizeof(struct _Mix_Channel));
    9.67 +	mix_channel = (struct _Mix_Channel *) SDL_malloc(num_channels * sizeof(struct _Mix_Channel));
    9.68  
    9.69  	/* Clear out the audio channels */
    9.70  	for ( i=0; i<num_channels; ++i ) {
    9.71 @@ -497,7 +497,7 @@
    9.72  		}
    9.73  	}
    9.74  	SDL_LockAudio();
    9.75 -	mix_channel = (struct _Mix_Channel *) realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
    9.76 +	mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
    9.77  	if ( numchans > num_channels ) {
    9.78  		/* Initialize the new channels */
    9.79  		int i;
    9.80 @@ -568,7 +568,7 @@
    9.81  	}
    9.82  
    9.83  	/* Allocate the chunk memory */
    9.84 -	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
    9.85 +	chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
    9.86  	if ( chunk == NULL ) {
    9.87  		SDL_SetError("Out of memory");
    9.88  		if ( freesrc ) {
    9.89 @@ -613,7 +613,7 @@
    9.90  			return(0);			
    9.91  	}
    9.92  	if ( !loaded ) {
    9.93 -		free(chunk);
    9.94 +		SDL_free(chunk);
    9.95  		if ( freesrc ) {
    9.96  			SDL_RWclose(src);
    9.97  		}
    9.98 @@ -632,26 +632,26 @@
    9.99  		if ( SDL_BuildAudioCVT(&wavecvt,
   9.100  				wavespec.format, wavespec.channels, wavespec.freq,
   9.101  				mixer.format, mixer.channels, mixer.freq) < 0 ) {
   9.102 -			SDL_FreeWAV(chunk->abuf);
   9.103 -			free(chunk);
   9.104 +			SDL_free(chunk->abuf);
   9.105 +			SDL_free(chunk);
   9.106  			return(NULL);
   9.107  		}
   9.108  		samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
   9.109  		wavecvt.len = chunk->alen & ~(samplesize-1);
   9.110 -		wavecvt.buf = (Uint8 *)calloc(1, wavecvt.len*wavecvt.len_mult);
   9.111 +		wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult);
   9.112  		if ( wavecvt.buf == NULL ) {
   9.113  			SDL_SetError("Out of memory");
   9.114 -			SDL_FreeWAV(chunk->abuf);
   9.115 -			free(chunk);
   9.116 +			SDL_free(chunk->abuf);
   9.117 +			SDL_free(chunk);
   9.118  			return(NULL);
   9.119  		}
   9.120  		memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
   9.121 -		SDL_FreeWAV(chunk->abuf);
   9.122 +		SDL_free(chunk->abuf);
   9.123  
   9.124  		/* Run the audio converter */
   9.125  		if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
   9.126 -			free(wavecvt.buf);
   9.127 -			free(chunk);
   9.128 +			SDL_free(wavecvt.buf);
   9.129 +			SDL_free(chunk);
   9.130  			return(NULL);
   9.131  		}
   9.132  	}
   9.133 @@ -677,7 +677,7 @@
   9.134  	}
   9.135  
   9.136  	/* Allocate the chunk memory */
   9.137 -	chunk = (Mix_Chunk *)calloc(1,sizeof(Mix_Chunk));
   9.138 +	chunk = (Mix_Chunk *)SDL_calloc(1,sizeof(Mix_Chunk));
   9.139  	if ( chunk == NULL ) {
   9.140  		SDL_SetError("Out of memory");
   9.141  		return(NULL);
   9.142 @@ -711,7 +711,7 @@
   9.143  	}
   9.144  
   9.145  	/* Allocate the chunk memory */
   9.146 -	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
   9.147 +	chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
   9.148  	if ( chunk == NULL ) {
   9.149  		SDL_SetError("Out of memory");
   9.150  		return(NULL);
   9.151 @@ -746,9 +746,9 @@
   9.152  		SDL_UnlockAudio();
   9.153  		/* Actually free the chunk */
   9.154  		if ( chunk->allocated ) {
   9.155 -			free(chunk->abuf);
   9.156 +			SDL_free(chunk->abuf);
   9.157  		}
   9.158 -		free(chunk);
   9.159 +		SDL_free(chunk);
   9.160  	}
   9.161  }
   9.162  
   9.163 @@ -1135,11 +1135,11 @@
   9.164  			Mix_HaltChannel(-1);
   9.165  			_Mix_DeinitEffects();
   9.166  			SDL_CloseAudio();
   9.167 -			free(mix_channel);
   9.168 +			SDL_free(mix_channel);
   9.169  			mix_channel = NULL;
   9.170  
   9.171  			/* rcg06042009 report available decoders at runtime. */
   9.172 -			free(chunk_decoders);
   9.173 +			SDL_free(chunk_decoders);
   9.174  			chunk_decoders = NULL;
   9.175  			num_decoders = 0;
   9.176  		}
   9.177 @@ -1311,7 +1311,7 @@
   9.178  		return(0);
   9.179  	}
   9.180  
   9.181 -	new_e = malloc(sizeof (effect_info));
   9.182 +	new_e = SDL_malloc(sizeof (effect_info));
   9.183  	if (new_e == NULL) {
   9.184  		Mix_SetError("Out of memory");
   9.185  		return(0);
   9.186 @@ -1358,7 +1358,7 @@
   9.187  			if (cur->done_callback != NULL) {
   9.188  				cur->done_callback(channel, cur->udata);
   9.189  			}
   9.190 -			free(cur);
   9.191 +			SDL_free(cur);
   9.192  
   9.193  			if (prev == NULL) {   /* removing first item of list? */
   9.194  				*e = next;
   9.195 @@ -1391,7 +1391,7 @@
   9.196  		if (cur->done_callback != NULL) {
   9.197  			cur->done_callback(channel, cur->udata);
   9.198  		}
   9.199 -		free(cur);
   9.200 +		SDL_free(cur);
   9.201  	}
   9.202  	*e = NULL;
   9.203  
    10.1 --- a/music.c	Fri Jan 13 02:39:41 2012 -0500
    10.2 +++ b/music.c	Fri Jan 13 03:15:19 2012 -0500
    10.3 @@ -163,7 +163,7 @@
    10.4  
    10.5  static void add_music_decoder(const char *decoder)
    10.6  {
    10.7 -	void *ptr = realloc(music_decoders, (num_decoders + 1) * sizeof (const char **));
    10.8 +	void *ptr = SDL_realloc(music_decoders, (num_decoders + 1) * sizeof (const char **));
    10.9  	if (ptr == NULL) {
   10.10  		return;  /* oh well, go on without it. */
   10.11  	}
   10.12 @@ -520,7 +520,7 @@
   10.13  #ifdef CMD_MUSIC
   10.14  	if ( music_cmd ) {
   10.15  		/* Allocate memory for the music structure */
   10.16 -		music = (Mix_Music *)malloc(sizeof(Mix_Music));
   10.17 +		music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
   10.18  		if ( music == NULL ) {
   10.19  			Mix_SetError("Out of memory");
   10.20  			return(NULL);
   10.21 @@ -529,7 +529,7 @@
   10.22  		music->type = MUS_CMD;
   10.23  		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
   10.24  		if ( music->data.cmd == NULL ) {
   10.25 -			free(music);
   10.26 +			SDL_free(music);
   10.27  			music == NULL;
   10.28  		}
   10.29  		return music;
   10.30 @@ -606,7 +606,7 @@
   10.31  	}
   10.32  
   10.33  	/* Allocate memory for the music structure */
   10.34 -	music = (Mix_Music *)malloc(sizeof(Mix_Music));
   10.35 +	music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
   10.36  	if (music == NULL ) {
   10.37  		Mix_SetError("Out of memory");
   10.38  		return NULL;
   10.39 @@ -745,7 +745,7 @@
   10.40  
   10.41  
   10.42  	if (music->error) {
   10.43 -		free(music);
   10.44 +		SDL_free(music);
   10.45  		music=NULL;
   10.46  	}
   10.47  	return(music);
   10.48 @@ -838,7 +838,7 @@
   10.49  		}
   10.50  
   10.51      skip:
   10.52 -		free(music);
   10.53 +		SDL_free(music);
   10.54  	}
   10.55  }
   10.56  
   10.57 @@ -1481,11 +1481,11 @@
   10.58  {
   10.59  	Mix_HaltMusic();
   10.60  	if ( music_cmd ) {
   10.61 -		free(music_cmd);
   10.62 +		SDL_free(music_cmd);
   10.63  		music_cmd = NULL;
   10.64  	}
   10.65  	if ( command ) {
   10.66 -		music_cmd = (char *)malloc(strlen(command)+1);
   10.67 +		music_cmd = (char *)SDL_malloc(strlen(command)+1);
   10.68  		if ( music_cmd == NULL ) {
   10.69  			return(-1);
   10.70  		}
   10.71 @@ -1527,7 +1527,7 @@
   10.72  #endif
   10.73  
   10.74  	/* rcg06042009 report available decoders at runtime. */
   10.75 -	free(music_decoders);
   10.76 +	SDL_free(music_decoders);
   10.77  	music_decoders = NULL;
   10.78  	num_decoders = 0;
   10.79  
   10.80 @@ -1538,12 +1538,12 @@
   10.81  {
   10.82  #ifdef MID_MUSIC
   10.83  	if (soundfont_paths) {
   10.84 -		free(soundfont_paths);
   10.85 +		SDL_free(soundfont_paths);
   10.86  		soundfont_paths = NULL;
   10.87  	}
   10.88  
   10.89  	if (paths) {
   10.90 -		if (!(soundfont_paths = strdup(paths))) {
   10.91 +		if (!(soundfont_paths = SDL_strdup(paths))) {
   10.92  			Mix_SetError("Insufficient memory to set SoundFonts");
   10.93  			return 0;
   10.94  		}
   10.95 @@ -1574,7 +1574,7 @@
   10.96  		return 0;
   10.97  	}
   10.98  
   10.99 -	if (!(paths = strdup(cpaths))) {
  10.100 +	if (!(paths = SDL_strdup(cpaths))) {
  10.101  		Mix_SetError("Insufficient memory to iterate over SoundFonts");
  10.102  		return 0;
  10.103  	}
  10.104 @@ -1587,12 +1587,12 @@
  10.105  	for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
  10.106  #endif
  10.107  		if (!function(path, data)) {
  10.108 -			free(paths);
  10.109 +			SDL_free(paths);
  10.110  			return 0;
  10.111  		}
  10.112  	}
  10.113  
  10.114 -	free(paths);
  10.115 +	SDL_free(paths);
  10.116  	return 1;
  10.117  }
  10.118  #endif
    11.1 --- a/music_cmd.c	Fri Jan 13 02:39:41 2012 -0500
    11.2 +++ b/music_cmd.c	Fri Jan 13 03:15:19 2012 -0500
    11.3 @@ -48,7 +48,7 @@
    11.4  	MusicCMD *music;
    11.5  
    11.6  	/* Allocate and fill the music structure */
    11.7 -	music = (MusicCMD *)malloc(sizeof *music);
    11.8 +	music = (MusicCMD *)SDL_malloc(sizeof *music);
    11.9  	if ( music == NULL ) {
   11.10  		Mix_SetError("Out of memory");
   11.11  		return(NULL);
   11.12 @@ -123,7 +123,7 @@
   11.13  	if ( last_arg ) {
   11.14  		++argc;
   11.15  	}
   11.16 -	argv = (char **)malloc((argc+1)*(sizeof *argv));
   11.17 +	argv = (char **)SDL_malloc((argc+1)*(sizeof *argv));
   11.18  	if ( argv == NULL ) {
   11.19  		return(NULL);
   11.20  	}
   11.21 @@ -219,7 +219,7 @@
   11.22  /* Close the given music stream */
   11.23  void MusicCMD_FreeSong(MusicCMD *music)
   11.24  {
   11.25 -	free(music);
   11.26 +	SDL_free(music);
   11.27  }
   11.28  
   11.29  /* Return non-zero if a stream is currently playing */
    12.1 --- a/music_flac.c	Fri Jan 13 02:39:41 2012 -0500
    12.2 +++ b/music_flac.c	Fri Jan 13 03:15:19 2012 -0500
    12.3 @@ -187,7 +187,7 @@
    12.4  
    12.5  				// create it
    12.6  				data->flac_data.data =
    12.7 -									(char *)malloc (data->flac_data.data_len);
    12.8 +					(char *)SDL_malloc (data->flac_data.data_len);
    12.9  
   12.10  				if (!data->flac_data.data) {
   12.11  					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   12.12 @@ -227,7 +227,7 @@
   12.13  
   12.14  				// make it big enough for the rest of the block
   12.15  				data->flac_data.overflow =
   12.16 -								(char *)malloc (data->flac_data.overflow_len);
   12.17 +					(char *)SDL_malloc (data->flac_data.overflow_len);
   12.18  
   12.19  				if (!data->flac_data.overflow) {
   12.20  					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   12.21 @@ -313,7 +313,7 @@
   12.22  		return NULL;
   12.23  	}
   12.24  
   12.25 -	music = (FLAC_music *)malloc ( sizeof (*music));
   12.26 +	music = (FLAC_music *)SDL_malloc ( sizeof (*music));
   12.27  	if (music) {
   12.28  		/* Initialize the music structure */
   12.29  		memset (music, 0, (sizeof (*music)));
   12.30 @@ -367,7 +367,7 @@
   12.31  					flac.FLAC__stream_decoder_delete( music->flac_decoder );
   12.32  				case 1:
   12.33  				case 0:
   12.34 -					free(music);
   12.35 +					SDL_free(music);
   12.36  					if (freerw) {
   12.37  						SDL_RWclose(rw);
   12.38  					}
   12.39 @@ -409,7 +409,7 @@
   12.40  	music->flac_data.data_len = music->flac_data.max_to_read;
   12.41  	music->flac_data.data_read = 0;
   12.42  	if (!music->flac_data.data) {
   12.43 -		music->flac_data.data = (char *)malloc (music->flac_data.data_len);
   12.44 +		music->flac_data.data = (char *)SDL_malloc (music->flac_data.data_len);
   12.45  	}
   12.46  
   12.47  	// we have data to read
   12.48 @@ -471,7 +471,7 @@
   12.49  		if (cvt->buf) {
   12.50  			free (cvt->buf);
   12.51  		}
   12.52 -		cvt->buf = (Uint8 *)malloc (music->flac_data.data_len * cvt->len_mult);
   12.53 +		cvt->buf = (Uint8 *)SDL_malloc (music->flac_data.data_len * cvt->len_mult);
   12.54  		music->section = 0;
   12.55  	}
   12.56  	if (cvt->buf) {
    13.1 --- a/music_mad.c	Fri Jan 13 02:39:41 2012 -0500
    13.2 +++ b/music_mad.c	Fri Jan 13 03:15:19 2012 -0500
    13.3 @@ -30,7 +30,7 @@
    13.4  {
    13.5    mad_data *mp3_mad;
    13.6  
    13.7 -  mp3_mad = (mad_data *)malloc(sizeof(mad_data));
    13.8 +  mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data));
    13.9    if (mp3_mad) {
   13.10  	mp3_mad->rw = rw;
   13.11  	mp3_mad->freerw = freerw;
   13.12 @@ -58,7 +58,7 @@
   13.13    if (mp3_mad->freerw) {
   13.14  	SDL_RWclose(mp3_mad->rw);
   13.15    }
   13.16 -  free(mp3_mad);
   13.17 +  SDL_free(mp3_mad);
   13.18  }
   13.19  
   13.20  /* Starts the playback. */
    14.1 --- a/music_modplug.c	Fri Jan 13 02:39:41 2012 -0500
    14.2 +++ b/music_modplug.c	Fri Jan 13 03:15:19 2012 -0500
    14.3 @@ -85,19 +85,19 @@
    14.4  	SDL_RWseek(rw, 0, RW_SEEK_END);
    14.5  	sz = SDL_RWtell(rw)-offset;
    14.6  	SDL_RWseek(rw, offset, RW_SEEK_SET);
    14.7 -	buf=(char*)malloc(sz);
    14.8 +	buf=(char*)SDL_malloc(sz);
    14.9  	if(buf)
   14.10  	{
   14.11  		if(SDL_RWread(rw, buf, sz, 1)==1)
   14.12  		{
   14.13 -			music=(modplug_data*)malloc(sizeof(modplug_data));
   14.14 +			music=(modplug_data*)SDL_malloc(sizeof(modplug_data));
   14.15  			if (music)
   14.16  			{
   14.17  				music->playing=0;
   14.18  				music->file=ModPlug_Load(buf,sz);
   14.19  				if(!music->file)
   14.20  				{
   14.21 -					free(music);
   14.22 +					SDL_free(music);
   14.23  					music=NULL;
   14.24  				}
   14.25  			}
   14.26 @@ -106,7 +106,7 @@
   14.27  				SDL_OutOfMemory();
   14.28  			}
   14.29  		}
   14.30 -		free(buf);
   14.31 +		SDL_free(buf);
   14.32  	}
   14.33  	else
   14.34  	{
   14.35 @@ -227,7 +227,7 @@
   14.36  void modplug_delete(modplug_data *music)
   14.37  {
   14.38  	ModPlug_Unload(music->file);
   14.39 -	free(music);
   14.40 +	SDL_free(music);
   14.41  }
   14.42  
   14.43  /* Jump (seek) to a given position (time is in seconds) */
    15.1 --- a/music_ogg.c	Fri Jan 13 02:39:41 2012 -0500
    15.2 +++ b/music_ogg.c	Fri Jan 13 03:15:19 2012 -0500
    15.3 @@ -84,7 +84,7 @@
    15.4  	callbacks.seek_func = sdl_seek_func;
    15.5  	callbacks.tell_func = sdl_tell_func;
    15.6  
    15.7 -	music = (OGG_music *)malloc(sizeof *music);
    15.8 +	music = (OGG_music *)SDL_malloc(sizeof *music);
    15.9  	if ( music ) {
   15.10  		/* Initialize the music structure */
   15.11  		memset(music, 0, (sizeof *music));
   15.12 @@ -95,7 +95,7 @@
   15.13  		music->section = -1;
   15.14  
   15.15  		if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
   15.16 -			free(music);
   15.17 +			SDL_free(music);
   15.18  			if ( freerw ) {
   15.19  				SDL_RWclose(rw);
   15.20  			}
   15.21 @@ -151,9 +151,9 @@
   15.22  		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   15.23  		                       mixer.format,mixer.channels,mixer.freq);
   15.24  		if ( cvt->buf ) {
   15.25 -			free(cvt->buf);
   15.26 +			SDL_free(cvt->buf);
   15.27  		}
   15.28 -		cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
   15.29 +		cvt->buf = (Uint8 *)SDL_malloc(sizeof(data)*cvt->len_mult);
   15.30  		music->section = section;
   15.31  	}
   15.32  	if ( cvt->buf ) {
   15.33 @@ -211,13 +211,13 @@
   15.34  {
   15.35  	if ( music ) {
   15.36  		if ( music->cvt.buf ) {
   15.37 -			free(music->cvt.buf);
   15.38 +			SDL_free(music->cvt.buf);
   15.39  		}
   15.40  		if ( music->freerw ) {
   15.41  			SDL_RWclose(music->rw);
   15.42  		}
   15.43  		vorbis.ov_clear(&music->vf);
   15.44 -		free(music);
   15.45 +		SDL_free(music);
   15.46  	}
   15.47  }
   15.48  
    16.1 --- a/wavestream.c	Fri Jan 13 02:39:41 2012 -0500
    16.2 +++ b/wavestream.c	Fri Jan 13 03:15:19 2012 -0500
    16.3 @@ -126,7 +126,7 @@
    16.4  		}
    16.5  		return(NULL);
    16.6  	}
    16.7 -	wave = (WAVStream *)malloc(sizeof *wave);
    16.8 +	wave = (WAVStream *)SDL_malloc(sizeof *wave);
    16.9  	if ( wave ) {
   16.10  		memset(wave, 0, (sizeof *wave));
   16.11  		wave->freerw = freerw;
   16.12 @@ -141,7 +141,7 @@
   16.13  			Mix_SetError("Unknown WAVE format");
   16.14  		}
   16.15  		if ( wave->rw == NULL ) {
   16.16 -			free(wave);
   16.17 +			SDL_free(wave);
   16.18  			if ( freerw ) {
   16.19  				SDL_RWclose(rw);
   16.20  			}
   16.21 @@ -181,10 +181,10 @@
   16.22  			if ( music->cvt.len != original_len ) {
   16.23  				int worksize;
   16.24  				if ( music->cvt.buf != NULL ) {
   16.25 -					free(music->cvt.buf);
   16.26 +					SDL_free(music->cvt.buf);
   16.27  				}
   16.28  				worksize = original_len*music->cvt.len_mult;
   16.29 -				music->cvt.buf=(Uint8 *)malloc(worksize);
   16.30 +				music->cvt.buf=(Uint8 *)SDL_malloc(worksize);
   16.31  				if ( music->cvt.buf == NULL ) {
   16.32  					return 0;
   16.33  				}
   16.34 @@ -238,12 +238,12 @@
   16.35  	if ( wave ) {
   16.36  		/* Clean up associated data */
   16.37  		if ( wave->cvt.buf ) {
   16.38 -			free(wave->cvt.buf);
   16.39 +			SDL_free(wave->cvt.buf);
   16.40  		}
   16.41  		if ( wave->freerw ) {
   16.42  			SDL_RWclose(wave->rw);
   16.43  		}
   16.44 -		free(wave);
   16.45 +		SDL_free(wave);
   16.46  	}
   16.47  }
   16.48  
   16.49 @@ -264,14 +264,14 @@
   16.50  	chunk->magic	= SDL_ReadLE32(src);
   16.51  	chunk->length	= SDL_ReadLE32(src);
   16.52  	if ( read_data ) {
   16.53 -		chunk->data = (Uint8 *)malloc(chunk->length);
   16.54 +		chunk->data = (Uint8 *)SDL_malloc(chunk->length);
   16.55  		if ( chunk->data == NULL ) {
   16.56  			Mix_SetError("Out of memory");
   16.57  			return(-1);
   16.58  		}
   16.59  		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
   16.60  			Mix_SetError("Couldn't read chunk");
   16.61 -			free(chunk->data);
   16.62 +			SDL_free(chunk->data);
   16.63  			return(-1);
   16.64  		}
   16.65  	} else {
   16.66 @@ -312,7 +312,7 @@
   16.67  	do {
   16.68  		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
   16.69  		if ( chunk.data ) {
   16.70 -			free(chunk.data);
   16.71 +			SDL_free(chunk.data);
   16.72  		}
   16.73  		lenread = ReadChunk(src, &chunk, 1);
   16.74  		if ( lenread < 0 ) {
   16.75 @@ -324,7 +324,7 @@
   16.76  	/* Decode the audio data format */
   16.77  	format = (WaveFMT *)chunk.data;
   16.78  	if ( chunk.magic != FMT ) {
   16.79 -		free(chunk.data);
   16.80 +		SDL_free(chunk.data);
   16.81  		Mix_SetError("Complex WAVE files not supported");
   16.82  		was_error = 1;
   16.83  		goto done;
   16.84 @@ -369,7 +369,7 @@
   16.85  
   16.86  done:
   16.87  	if ( format != NULL ) {
   16.88 -		free(format);
   16.89 +		SDL_free(format);
   16.90  	}
   16.91  	if ( was_error ) {
   16.92  		return NULL;