Question: I’m wondering if I messed up how I used SDL_Mixer.
Problem: When I call Mix_PlayMusic my program crashes, and when I don’t it runs perfectly fine. None of the Mix functions are returning any errors. Loading music is fine, loading and playing sound effects works. Only when I actually call Mix_PlayMusic does the program crash. There’s a lot of files in the project but I’ve pinpointed the error and it seems this function is corrupting memory as I’ll explain below.
Error Details: Object A stores pointer to Object B and calls a function of Object B. That function calls Mix_PlayMusic. Object A also stores a pointer to Object C, Object C stores a const char** (array of c-style strings). Before Object A calls the function of Object B the const char** array of Object C is full and I can print each string. Directly after the function call (function only tries to play music) the 5th Index and onwards cannot be printed nor are they NULL (printing crashes program). Note, if I avoid the corrupted memory the music plays correctly.
Thank you for considering my problem.
Libraries used by project: SDL2, SDL_Mixer, SDL_GPU, SDL_NFont, SDL_TTF, SDL_Image, TinyXML2
This is order of libraries initialization…
GPU_SetDebug(), GPU_Init(), SDL_Init (audio and joystick), Mix_Init(MIX_INIT_MP3), Mix_OpenAudio(defaults), Img_Init(PNG), TTF_Init(), SDL_OpenJoystick().
int flags = MIX_INIT_MP3;
int initted = Mix_Init(flags);
if(initted&flags != flags)
printf("Mix_Init: Failed to init mp3 support!\n");
printf("Mix_Init: %s\n", Mix_GetError());
int nc = 1; // mono
nc = 2; // stereo
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, nc, 2048 ) < 0 )
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
These are Mix functions used…
Call Mix_LoadWav() and Mix_PlayChannel for sfx, Mix_LoadMus() and Mix_PlayMusic() for music (note Mix_Chunk** and Mix_Music** used)
resultArray[i] = Mix_LoadWAV(child->GetText());
Mix_PlayChannel(-1, setSound->soundsArray, 0);
mArray[i] = Mix_LoadMUS(child->GetText());
printf("Mix_LoadMUS(\"%s\"): %s\n", child->GetText(), Mix_GetError());
if(Mix_PlayMusic(musicArray[index], -1) == -1)
printf("Mix_PlayMusic: %s\n", Mix_GetError());