We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 369

Summary: Mix_LoadWAV_RW cannot load the OGG file.
Product: SDL_mixer Reporter: aki. <c>
Component: miscAssignee: Ryan C. Gordon <icculus>
Status: VERIFIED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: gcarlton
Version: unspecified   
Hardware: x86   
OS: Other   
Attachments: Fix audio buffer being freed in the wrong library.

Description aki. 2006-12-01 23:35:34 UTC
It crashes in SDL_FreeWAV(chunk->abuf)
 when the OGG file is passed to Mix_LoadWAV_RW().

It can be fixed by inserting "memset(chunk, 0, sizeof(Mix_Chunk));"
 before a comment of "/* Find out what kind of audio file this is */."
Comment 1 Patrice Mandin 2006-12-19 14:55:25 UTC
Fixed in svn, revision 2929
Comment 2 aki. 2006-12-19 23:21:19 UTC
Confirmed it. 
Thanks for your work.
Comment 3 Ryan C. Gordon 2007-02-13 02:55:23 UTC
*** Bug 307 has been marked as a duplicate of this bug. ***
Comment 4 Daniel Liptrot 2010-07-19 15:21:45 UTC
Created attachment 523 [details]
Fix audio buffer being freed in the wrong library.

Sorry to hijack this bug, but I believe it may not be fixed from the problems I'm getting, which seems identical to the symptoms here. I wasn't sure whether to hijack this or file a new bug, as the bug is slightly larger in scope than what is reported here. Here is probably the best place.

The issue seems to be that the audio buffer is being freed in the wrong library. When sounds are played in the WAV or RIFF format, SDL_LoadWAV_RW() is called which is part of the SDL shared library, and the audio buffer is allocated there. Calling SDL_FreeWAV() then frees the buffer in that library. However, when other formats are played such as OGG, the audio buffer is allocated in the SDL_mixer library instead, and SDL_FreeWAV() is still called. This attempts to free the buffer in the wrong library as it is not allocated there. It can result in memory corruption and a possible memory leak as the audio buffer in the SDL_mixer library is not freed.

The bug is slightly larger in scope because a similar thing happens in Mix_FreeChunk(). This time, free() is used to free the audio buffer, which frees it in the SDL_mixer library. However, it also means if the WAV or RIFF format is used for the chunk, the audio buffer is freed in the wrong library and could result in memory corruption and a memory leak.

The fix for this would be to call the correct free function depending on which library the audio buffer was allocated. This seems to fix the crashes I've been getting.

This patch is what I've done to my working copy. It should work for both 1.2.11 and the latest hg revision as the line numbers are identical. It adds a new internal function which acts as a wrapper to call the correct free function for the audio buffer. The Mix_Chunk struct was also modified to store whether or not the audio buffer is allocated in the SDL shared library or not. It's an API change but one which should retain backwards compatibility with all software that uses SDL_mixer. Hopefully, that's classed as acceptable. :)