[SDL_Mixer] Music ownership inconsistencies

When I create a SDL_rwops and pass it in to SDL_mixer, who cleans it up and when?? Good question.? The answer is, it depends on the file type.

For a WAV file, the library keeps the RW open the whole time it’s playing back in order to stream the contents.? For a MOD file, it loads the whole song into memory at once and no longer cares about the RW.? For an OGG file, it keeps the RW open and streams the contents, then calls the RW’s Close method (which the WAV playback does not do) when the song is finished playing.? For an MP3… I’m not sure.? I haven’t been able to get SMPEG to work right with SDL 1.3/SDL_Mixer.

With my RWs being custom-built as wrappers around Delphi stream objects that need to be cleaned up properly themselves, this is a real headache.? Any way we could make the behavior a bit more consistent?

How about this:? When SDL_Mixer is finished with the RW, (whenever that may be, depending on the music type,) it always calls the RW’s Close method.? After that, it’s safe to free, and it’s the caller’s responsibility to call SDL_FreeRW on it.? Sound like a good way to handle it?

Hey, you just figured out why my apps leak memory like a b**ch :stuck_out_tongue: When
I was freeing the rwops, it would crash, so I didn’t free it. Your
observation that some loaders close the rwops and some don’t comes as a
big surprise to me.On 06/21/2011 06:49 AM, Mason Wheeler wrote:

When I create a SDL_rwops and pass it in to SDL_mixer, who cleans it up
and when? Good question. The answer is, it depends on the file type.
[…]

Yeah, that would do it.? I’ve got an all-purpose solution, but it requires you to build your own RWs:

In the Close method, free the RW’s data and then set it to NULL.? Make sure you keep the RW around in your code until SDL_Mixer notifies you that its song is done playing. At that point, call the Close method, then SDL_FreeRW.? Since it’s always safe to try to Free a null pointer, this way you can make sure everything gets cleaned up with no double-free errors, even if SDL_Mixer calls Close first.

I’d really prefer to have it work by fixing the problem inside of SDL_Mixer, though.________________________________
From: realnc@arcor.de (Nikos Chantziaras)
Subject: Re: [SDL] [SDL_Mixer] Music ownership inconsistencies

On 06/21/2011 06:49 AM, Mason Wheeler wrote:

When I create a SDL_rwops and pass it in to SDL_mixer, who cleans it up
and when? Good question. The answer is, it depends on the file type.
[…]

Hey, you just figured out why my apps leak memory like a b**ch :-P? When I was freeing the rwops, it would crash, so I didn’t free it.? Your observation that some loaders close the rwops and some don’t comes as a big surprise to me.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I’d really prefer to have it work by fixing the problem inside of
SDL_Mixer, though.

It needs to be fixed in SDL_mixer. Most RWOPS also free() themselves on
close, which means that the second attempt to close it will result in a
free() of a bogus pointer, even if its internal data was NULL’d out.

I didn’t realize this was such a mess in SDL_mixer. We should be very
clear about who is responsible for cleanup, and when cleanup is
done…and we should fix code in SDL_mixer that breaks those rules.

–ryan.

I’d really prefer to have it work by fixing the problem inside of

SDL_Mixer, though.

It needs to be fixed in SDL_mixer. Most RWOPS also free() themselves on
close, which means that the second attempt to close it will result in a
free() of a bogus pointer, even if its internal data was NULL’d out.

I didn’t realize this was such a mess in SDL_mixer. We should be very clear
about who is responsible for cleanup, and when cleanup is done…and we
should fix code in SDL_mixer that breaks those rules.

–ryan.

I’ve been poking around with this in my spare time. You can browse my repo
at http://remote.cogwheel.info/hg

See also
http://lists.libsdl.org/htdig.cgi/sdl-libsdl.org/2011-April/080525.htmlOn Mon, Jun 20, 2011 at 9:43 PM, Ryan C. Gordon wrote: