Skip to content

Commit

Permalink
fix segfault when cleaning up MixChunks on windows
Browse files Browse the repository at this point in the history
We built the libSDL binaries several years ago, and so it was built
against an older runtime library (msvcrt.dll).
Now we build SDL itself against msvcr110.dll for example, and therefore
allocate on another heap than the free() is taking place, which happens
in libSDL_mixer itself.
See: http://stackoverflow.com/questions/23257226/sdl-mixer-mix-freechunk-crashing-on-sample-created-in-memory
  • Loading branch information
FROGGS authored and wchristian committed Mar 14, 2015
1 parent 0bb1af8 commit ae531d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Mixer/Channels.xs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,13 @@ mixchan_get_chunk(chan)
char* CLASS = "SDL::Mixer::MixChunk";
CODE:
Mix_Chunk *chunk = Mix_GetChunk(chan);
#ifdef _WIN32
Mix_Chunk *copy = msvcrt_malloc(sizeof(Mix_Chunk));
copy->abuf = msvcrt_malloc( chunk->alen );
#else
Mix_Chunk *copy = malloc(sizeof(Mix_Chunk));
copy->abuf = malloc( chunk->alen );
#endif
memcpy( copy->abuf, chunk->abuf, chunk->alen );
copy->alen = chunk->alen;
copy->volume = chunk->volume;
Expand Down
13 changes: 13 additions & 0 deletions src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
#ifndef SDL_PERL_DEFINES_H
#define SDL_PERL_DEFINES_H

#ifdef _WIN32
#include <windows.h>
typedef void* (*malloc_t)(int size);
malloc_t _msvcrt_malloc = NULL;
extern malloc_t _msvcrt_malloc;

void *msvcrt_malloc(int size) {
if (!_msvcrt_malloc)
_msvcrt_malloc = (malloc_t)GetProcAddress(GetModuleHandle("msvcrt"), "malloc");
return _msvcrt_malloc(size);
}
#endif

#ifdef USE_THREADS
PerlInterpreter *parent_perl = NULL;
extern PerlInterpreter *parent_perl;
Expand Down

0 comments on commit ae531d0

Please sign in to comment.