SDL digest, Vol 1 #822 - 32 msgs

Message: 19

I finally got the time to track down the line of code which causes the
segfault when switching MIDI songs. It’s "CloseComponent(gTunePlayer)"
in native_midi_freesong() in native_midi_mac.c. This is a QuickTime
function call, so this is as deep as I can debug. I have no idea why
this causes segmentation faults, neither why it seems to happen only on
my machine…


This is likely a stack/heap overflow problem. Which probably explains
why it is so hard to reproduce and track down. You might find the
overflow is in your program which is triggering the failure later in

You may want to experiment with the MallocDebug application. To do
this, just link your program with -lMallocDebug. Then you can
launch/attach to your app in the MallocDebug application. MallocDebug
can do several helpful things that may or may not help - make sure to
read the manual to see all that it’s capable of. In many cases, you’ll
find that running with MallocDebug causes the problem to go away. This
is because MallocDebug is putting “guard” memory around each block it
allocates to catch any small overflows before they corrupt the heap.

Another thing I’d try is setting a watchpoint in gdb on the memory
address that malloc reports has been freed from under you (the free()
on an already freed block warning shows the address). Then you might be
able to pinpoint the code that is corrupting the heap. Project Builder
doesn’t have a UI for this yet. You do this with (for example)

watch (int*)(0xbfffe0a8 - 4)

To watch the integer just before the block starts.

You might also want to watch for overflows past the end of the block
(say if the block is 100 bytes):

watch (int*)&(((char*)0xbfffe0a8)[100])

You would do these commands immediately after allocating the block,
then you can see each time the memory has changed. This is just the
beginning of what gdb can do, but I hope this helps.

Other things to check:

  • buffer overflow in previously allocated block (not the one that
    mallocdebug reports) can corrupt malloc’s data structures.

It can be a hellish experience trying to do this kind of debugging for
the first time. If you need more help, drop me an email off-list.On Sunday, July 6, 2003, at 08:11 AM, sdl-request at wrote:

To: sdl at
From: Lex <dr_lex at>
Date: Sun, 06 Jul 2003 01:21:09 +0200
Subject: [SDL] malloc problems with SDL_mixer under OS X [2]
Reply-To: sdl at