Followers 0

# FMOD causing memory leaks?

## 17 posts in this topic

Hello!  I was using GlowCode to find memory leaks and saw that FMOD seems to be causing some:

Any help? Thanks!

Edited by KoldGames
0

##### Share on other sites

I can't see anything from the pictures, so this is 100% blind guesswork.

First, I'll guess you're talking about the sound library since obviously the FMOD instruction can't cause a leak and no other fmod jumps out at me. If that's the case, are you calling FMOD::Sound::release() after you finish using a sound? Being able to read the pictures might provide a better idea what functions you're calling and not calling for a better guess, but I just can't read the text in those pictures at all.

0

##### Share on other sites

Without really knowing anything about FMOD, it looks like the supposed leaks are in its initialization functions. Are you sure you are doing everything you need to do to properly unintialize FMOD prior to exiting your game?

0

##### Share on other sites

Are you calling System::release() when you're exiting your program? It looks like all of the leaks are from init(), so it's not memory constantly leaking or something. It's not even memory you'd ever have available for use during normal execution of your program (unless you release and create FMOD a lot, which you shouldn't).

If you are calling release(), make sure you're not nulling any relevant pointers or stomping on FMOD's memory or such before calling release() since you had your entire program's execution time to accidentally corrupt the data needed by FMOD to shutdown correctly.

0

##### Share on other sites

Are you sure you are doing everything you need to do to properly unintialize FMOD prior to exiting your game?

Yep, I believe so.  I call System::Release(), then set System to NULL, and delete the pointer.

If you are calling release(), make sure you're not nulling any relevant pointers or stomping on FMOD's memory or such before calling release() since you had your entire program's execution time to accidentally corrupt the data needed by FMOD to shutdown correctly.

I tried removing any FMOD related code from my application while only leaving the NXAudio's Initialize, Update, and Dispose functions, but still have a memory leak.  Very weird.

Here's my NXAudio class code:

FMOD::System* NXAudio::System = nullptr;
FMOD_RESULT result;

bool NXAudio::Initialize()
{
ZeroMemory(&System, sizeof(FMOD::System*));

result = FMOD::System_Create(&System);
if (result != FMOD_OK)
return false;

result = System->init(100, FMOD_INIT_NORMAL, 0);
if (result != FMOD_OK)
return false;

return true;
}

FMOD_RESULT NXAudio::Update()
{
return System->update();
}

void NXAudio::Dispose()
{
if (System)
{
System->release();
System = nullptr;
delete System;
}

return;
}

FMOD_RESULT NXAudio::CreateSound(const char* name, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO* exInfo, FMOD::Sound** sound)
{
return System->createSound(name, mode, exInfo, sound);
}

FMOD_RESULT NXAudio::CreateStream(const char* name, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO* exInfo, FMOD::Sound** sound)
{
return System->createStream(name, mode, exInfo, sound);
}

FMOD_RESULT NXAudio::PlaySound(FMOD::Sound* sound, FMOD::ChannelGroup* channelGroup, bool paused, FMOD::Channel** channel)
{
return System->playSound(sound, channelGroup, paused, channel);
}


The ZeroMemory line wasn't there before, I added that last night when I was trying to fix the leak.  The leak won't go away with or without.  I set FMOD up exactly like the documentation says (aside from having it in a static class).  Could the fact that System is static be the problem?

Edited by KoldGames
0

##### Share on other sites

I set FMOD up exactly like the documentation says (aside from having it in a static class).  Could the fact that System is static be the problem?

While it may be bad practice, it shouldn't really make a difference, since System is just a raw pointer (and so you're not relying on any kind of automatic destruction). You've verified by stepping through the debugger that NXAudio::Dispose is called and System->release gets called?

0

##### Share on other sites

You've verified by stepping through the debugger that NXAudio::Dispose is called and System->release gets called?

Yep!  I put a breakpoint on it and it is being called.

0

##### Share on other sites

Here's my NXAudio class code:

		void NXAudio::Dispose()
{
if (System)
{
System->release();
System = nullptr;
delete System;
}

return;
}



Delete should be before setting to nullptr.

0

##### Share on other sites

Delete should be before setting to nullptr.

When deleting before setting to nullptr, I get this error:

0

##### Share on other sites

Then you have a memory overflow bug somewhere in your application that is corrupting memory around the System object, you are deleting it twice, or the pointer was not created via new.

Since it was created via FMOD::System_Create(), why are you deleting it?  Tutorials show to just call System->release().

L. Spiro

Edited by L. Spiro
1

##### Share on other sites

why are you deleting it?

I'm still learning C++ and thought I had to delete it because it was a pointer. lol

0

##### Share on other sites

I would recommend grabbing the return value from System::release(). You might get an error from that which would tell you what is wrong. In any case, an error on release() would probably result in leaks.

I might be wrong on this, so you'll have to do a little homework to make sure. I think you're supposed to have a close() call paired with the init(), and release() gets paired with the create(). You'll also really want to pay attention to the errors that get returned for every part you shut down. Later, you'll probably have quite a few components to shutdown and they have to be in the right order. You'll also have calls that can fail if the hardware is still in use from a prior sound call, and other stuff like that.

0

##### Share on other sites

Just a stab in the dark, but are you allocating something and putting it in 'System' before you pass 'System' to FMOD::System_Create()? If so, then don't (that would cause a memory leak).

A few miscellanea:

*Don't ZeroMemory() the pointer. That's just silly.

*As was indicated, if you set the pointer to null and then delete it, you should be getting a segfault. Don't do that. (There's no reason to do either of those things. FMOD will free the memory it allocated when you call System->release().)

*No offense, but why does this class exist? All it appears to do is act as more-or-less a direct pass-thru for the FMOD system interface.

Please read the documentation that came with FMOD about how to start up the system correctly. It's on page 8 of "Getting started with FMOD for Windows.pdf".

Please research the RAII design pattern. Mastering RAII is the 'correct' way to avoid memory leaks in C++, and it also helps with things like exception safety and even makes it easier and more natural-feeling to use your objects!

1

##### Share on other sites

Oh, right. I forgot that. derp

0

##### Share on other sites

Sorry it took so long to reply!  I've been busy. I have a question.  If the memory leaks only happen during initialization, is that a big problem? As long as my application is not creating leaks during runtime, am I correct?  I noticed that on my applications startup with GlowCode enabled, it finds the leaks when I search, but if I click Clear, and search for memory leaks then, I get 1 leak with VSync off (On my swapchain's Present method) and 0 with VSync on.  Thanks!

Edited by KoldGames
0

##### Share on other sites

If the memory leaks only happen during initialization, is that a big problem?

All leaks should be treated seriously. It is extremely bad practice to let the operating system clean up your messes on application shut-down and I would never hire someone who disagreed with that.
The people who do that will be the ruin of your project some day.

L. Spiro
0

## Create an account

Register a new account