[Solved] Mix_PlayMusic Causing Memory Problems?

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.

Code Stuff…
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().

// Mix_Init
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());
}

// Mix_Open_Audio
int nc = 1; // mono
if(stereoMono)
{
    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], 0);

mArray[i] = Mix_LoadMUS(child->GetText());
if(!mArray[i])
{
    printf("Mix_LoadMUS(\"%s\"): %s\n", child->GetText(), Mix_GetError());            
}

if(Mix_PlayMusic(musicArray[index], -1) == -1)
{
    printf("Mix_PlayMusic: %s\n", Mix_GetError());
}

2.0.6 has broken SDL_mixer, specifically music.

Try AUDIO_F32SYS instead of MIX_DEFAULT_FORMAT. Try the old 2.0.5 SDL.DLL; if that works then it’s the bug I reported here https://bugzilla.libsdl.org/show_bug.cgi?id=3853

For me, it silences all the audio until I halt the music, so maybe you’re doing something else wrong.

Thanks for the reply.
AUDIO_F32SYS instead of MIX_DEFAULT_FORMAT doesn’t solve the problem and I’ve only used SDL 2.0.5 and the SDL_Mixer released at that time. So I guess its something else.

I’ll be honest: I can’t understand your code. Your array index “i” is used to access both your array of WAVs and MUSics in one go? You’re loading and storing both a sound effect, and a music file of the same filename?

Have you tried different music formats? Have you tried just loading a MUS and playing it in main, without any other code?

The lines of code provided are each in separate methods. I just wanted to be sure that the methods used and parameters were correct. I tried the OGG file type which corrupted the memory into weird symbols, but the program didn’t crash (NFont displayed strings as boxed question marks). I ran the music player in a much simpler program and nothing seemed wrong. I guess somehow its my code independent of SDL_Mixer but the problem surfaces when Mix_PlayMusic is used. Thanks for help.

Found Solution by Dr Memory. SDL_Mixer was unrelated to the problem, my understanding of TinyXML2 was. The c-style strings I was talking about were const char* pointers. These pointers were pointing to the Text returned from XMLElement* GetText(). These XMLElements when deleted also delete the contents of their text pointers so my c-style strings were returning NULL.
I have no idea how the program was able to run when music was not playing.

Glad you find it. Stepping through the code in a debugger to check values of variables are what you expect them to be usually shows what’s up.