For the past few months I’ve been learning SDL 1 from this book I bought. I’m still pretty new, but I have managed to build a few cool little games. I just got to the chapter in my book about “SDL_Mixer” and I set up the mixer framework so I could start adding sounds to my games. I added some really simple code to one of my games, just so I’d get a sound when the user scores a point. But now when I try to run it, the SDL window opens for a half a second, and then I get this message in the Xcode console:
SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object 0x101f4ebf0: pointer being freed was not allocated
I know the framework is set up right, because the example program that comes with my book runs fine. So I guess I must have messed up the code. I was hoping that someone could take a look at this – this is all the SDL_Mixer code that I added to my game, and since my game was running fine before I added this code, I’m assuming the problem must be here:
At the top:
Code:
#include “sdl_mixer/sdl_mixer.h”
Then, after the headers:
Code:
Mix_Chunk *succeedSnd = NULL;
Then, in my init() function:
Code:
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096) == -1)
{
return false;
}
if (Mix_AllocateChannels(2) == -1)
{
return false;
}
Then, in my load_files() function:
Code:
succeedSnd = Mix_LoadWAV(“succeed.wav”);
if (succeedSnd == NULL)
{
return false;
}
Then, in my cleanup() function:
Code:
Mix_AllocateChannels(0);
Mix_FreeChunk(succeedSnd);
Mix_CloseAudio();
And finally, in my increaseDiff() function, which gets called whenever the user scores a point:
Code:
Mix_PlayChannel(-1, succeedSnd, 0);
Anyone see any problems there? From the error message I got, I’m guessing there must be something wrong with my chunk pointer, but I don’t see it. Up until now, when I got errors, they were usually compile errors, and Xcode would highlight the problematic code, and I could usually spot the mistake easily enough. But I really don’t know how to handle this kind of error. The Xcode console is telling me “*** set a breakpoint in malloc_error_break to debug” but I really don’t know how to set up breakpoints, or even what they are, really. I tried reading the Xcode documentation, but it assumes a level of competency that I don’t yet have. Sigh
Thanks for your help.