• Create Account

## FMOD causing memory leaks?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

17 replies to this topic

### #1KoldGames  Members

Posted 12 January 2014 - 12:30 PM

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

Any help? Thanks!

Edited by KoldGames, 12 January 2014 - 12:31 PM.

### #2richardurich  Members

Posted 12 January 2014 - 01:31 PM

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.

### #3KoldGames  Members

Posted 12 January 2014 - 01:40 PM

Hey, sorry about that.  Here's some clearer pictures:

http://imageshack.com/scaled/large/829/przu.png

http://imageshack.com/scaled/large/202/erwj.png

http://imageshack.com/scaled/large/46/bbe6.png

I'm new to finding and fixing memory leaks. lol

### #4phil_t  Members

Posted 12 January 2014 - 04:56 PM

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?

### #5richardurich  Members

Posted 12 January 2014 - 04:57 PM

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.

### #6KoldGames  Members

Posted 12 January 2014 - 05:38 PM

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, 12 January 2014 - 05:42 PM.

### #7phil_t  Members

Posted 12 January 2014 - 06:37 PM

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?

### #8KoldGames  Members

Posted 12 January 2014 - 07:30 PM

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.

### #9richardurich  Members

Posted 12 January 2014 - 07:51 PM

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.

### #10KoldGames  Members

Posted 12 January 2014 - 07:55 PM

Delete should be before setting to nullptr.

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

### #11L. Spiro  Members

Posted 12 January 2014 - 08:26 PM

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, 12 January 2014 - 08:30 PM.

### #12KoldGames  Members

Posted 12 January 2014 - 08:41 PM

why are you deleting it?

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

### #13richardurich  Members

Posted 12 January 2014 - 10:06 PM

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.

### #14Khatharr  Members

Posted 12 January 2014 - 11:28 PM

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!

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #15L. Spiro  Members

Posted 12 January 2014 - 11:37 PM

if you set the pointer to null and then delete it, you should be getting a segfault.

NULL pointers are always safe to delete/free().

L. Spiro

### #16Khatharr  Members

Posted 14 January 2014 - 06:05 AM

Oh, right. I forgot that. derp

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #17KoldGames  Members

Posted 21 January 2014 - 08:45 AM

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, 21 January 2014 - 09:30 AM.

### #18L. Spiro  Members

Posted 21 January 2014 - 08:02 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.