Keytotruth
Keytotruth

Reputation: 31

FMOD Ex Memory Allocation Issue

I have a certain problem when using FmodEx. I've searched thoroughly over the net to see if someone had my same problem but I didn't find anything related to it. I made a class that loads and plays my sounds, in this case, streams. Here is my code:

Audio::Audio()
{
//Create system object//
m_Result = FMOD::System_Create(&m_pSystem);
ErrorCheck(m_Result);

//Check FMOD version//
m_Result = m_pSystem->getVersion(&m_FmodVersion);
if(m_FmodVersion < FMOD_VERSION)
    MessageBox(NULL, FMOD_ErrorString(m_Result), "FMOD Version Error", MB_OK);

//Check if hardware acceleration is disabled//
m_pSystem->getDriverCaps(0, &m_Caps, 0, &m_SpeakerMode);
if (m_Caps & FMOD_CAPS_HARDWARE_EMULATED)
    MessageBox(NULL, FMOD_ErrorString(m_Result), "FMOD Acceleration Error", MB_OK);

//Initialize system object//
m_Result = m_pSystem->init(2, FMOD_INIT_NORMAL, 0);
ErrorCheck(m_Result);

m_pChannel = 0;
m_IsLoaded = false;
}

void Audio::LoadMusic(char *filename)
{
m_Result = m_pSystem->createStream(filename, FMOD_CREATESTREAM, 0, &m_pSound);
ErrorCheck(m_Result);
}

void Audio::Play()
{
SetPause(false);
m_Result = m_pSystem->playSound(FMOD_CHANNEL_FREE, m_pSound, false, &m_pChannel);
ErrorCheck(m_Result);
SetPause(true);
}

After this I just do:

pAudio->LoadMusic("test.mp3");
pAudio->Play();

The sound plays no problem. The problem happens when loading the stream. The memory used keeps increasing all the time and it won't stop. I'm guessing that this happens because the small buffer it's using to read the mp3 stream is not beeing freed, thus, it looks for the next available piece of free memory in the RAM, thus the memory usage of the program doesn't stop increasing. I thought that maybe using the "release" method after each play would work, but then I noticed that release frees ALL the memory in the sound instance.

Could anyone give me some pointers on to what I'm doing wrong here? How do I prevent this? I'm not sure if I have made it clear enough or not. Thanks in advance for the help.

Upvotes: 3

Views: 1009

Answers (1)

Mathew Block
Mathew Block

Reputation: 1643

Each time you call pAudio->LoadMusic you will allocate (leak) more memory because you are creating a new FMOD::Sound instance (which as you indicate has its own stream buffer). If you simply want to play the sound again, just call pAudio->Play and the stream will restart.

If you are concerned about FMOD memory usage you can call Memory_GetStats to monitor it, just in case I have miss-understood your usage and something else is causing the leak.

Upvotes: 1

Related Questions