It seems that the problem is with multiple heaps on Windows.
When I analyzed the sample code in the debugger, the SDL_mixer function Mix_FreeChunk()
used free()
from msvcrt.dll, while the calling code called malloc()
from one of the modern Visual C Runtime Libraries (like msvcr110.dll or static version).
It doesn't make difference if you call SDL_malloc()
for this, because it is indeed just #define SDL_malloc malloc
.
The one solution I see is just to force use the malloc()
from msvcrt.dll too:
#include <windows.h>
...
typedef void* (*malloc_t)(size_t);
malloc_t msvcrt_malloc = reinterpret_cast<malloc_t>(
GetProcAddress(GetModuleHandle(TEXT("msvcrt")), "malloc"));
...
Mix_Chunk* s2 = (Mix_Chunk*)msvcrt_malloc(sizeof(Mix_Chunk));
s2->abuf = (Uint8*)msvcrt_malloc(s2->alen * sizeof(Uint8));
The program stopped crashing then for me.
Better option might be to just compile the SDL_mixer and/or SDL libraries yourself, with exactly the same environment settings as your final application.