It is actually safe to free chunks which are still playing

Though documentation and even a comment at the start of Mix_FreeChunk()
state that it's unsafe to free chunk which is still being played, the
function actually contains the code to stop all playback of a chunk which is
being freed. See SDL2_mixer 2.0.0, mixer.c:759:

    /* Guarantee that this chunk isn't playing */
    SDL_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;
            }
        }
    }
    SDL_UnlockAudio();

As a result, no special actions are required to stop Mixer playback
before destruction of the Chunks (which is a common case with SDL2pp,
as Chunks may only be constructed after Mixer is created, and are
correspondingly destroyed in reverse order, e.g. before Mixer).
This commit is contained in:
Dmitry Marakasov 2015-08-29 02:34:16 +03:00
parent 1cc64cea4f
commit e37d67725e
2 changed files with 7 additions and 3 deletions

View File

@ -80,6 +80,13 @@ public:
///
/// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC24
///
/// \note Despite what Mix_FreeChunk() documentation (and even
/// comment in function implementation) says, right after
/// the named comment there's a code which stops playback
/// of any channel which plays the chunk being freed.
/// Thus, it is safe to destroy Chunk before destroying
/// Mixer, even if it still plays the chunk.
///
////////////////////////////////////////////////////////////
~Chunk();

View File

@ -73,9 +73,6 @@ int main() try {
SDL_Delay(2000);
// Make sure no sounds are being played before destroying Chunk
mixer.HaltChannel();
return 0;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;