Mix_FreeChunk documentation says “It’s a bad idea to free a chunk that is still being played.”
However, while the source (mixer.c) does say “Caution – if the chunk is playing, the mixer will crash”, it then also “Guarantee[s] that this chunk isn’t playing”, in a way that look similar to the way Mix_FreeMusic is implemented (See code snippet below), and seems identical to the method used in Mix_HaltChannel.
So: is it actually ok to Mix_FreeChunk while the chunk is playing? I’ve tried it (several thousand times in a tight loop), and it doesn’t seem to cause problems.
void Mix_FreeChunk(Mix_Chunk *chunk)
{
int i;
/* Caution -- if the chunk is playing, the mixer will crash */
if (chunk) {
/* Guarantee that this chunk isn't playing */
Mix_LockAudio();
if (mix_channel) {
for (i=0; i<num_channels; ++i) {
if (chunk == mix_channel[i].chunk) {
mix_channel[i].playing = 0;
mix_channel[i].looping = 0;
}
}
}
Mix_UnlockAudio();
/* Actually free the chunk */
if (chunk->allocated) {
SDL_free(chunk->abuf);
}
SDL_free(chunk);
}
}
//And, for comparison, Mix_HaltChannel:
/* Halt playing of a particular channel */
int Mix_HaltChannel(int which)
{
int i;
if (which == -1) {
for (i=0; i<num_channels; ++i) {
Mix_HaltChannel(i);
}
} else if (which < num_channels) {
Mix_LockAudio();
if (mix_channel[which].playing) {
_Mix_channel_done_playing(which);
mix_channel[which].playing = 0;
mix_channel[which].looping = 0;
}
mix_channel[which].expire = 0;
if(mix_channel[which].fading != MIX_NO_FADING) /* Restore volume */
mix_channel[which].volume = mix_channel[which].fade_volume_reset;
mix_channel[which].fading = MIX_NO_FADING;
Mix_UnlockAudio();
}
return(0);
}