Search code examples
c++audiofmod

FMOD leaking while playing sound


I am doing a little game and it would be nice to add sound...

But I am leaking quite a lot, and even though I tried to delete everything and look for informations on the internet...

Here is my code :

#include "../../api/inc/fmod.hpp"
#include "../../api/inc/fmod_errors.h"                                                                                                                                                       
#include <iostream>
#include <string>

int     main()
{
  FMOD::System *system = NULL;
  FMOD::System_Create(&system);
  system->init(100, FMOD_INIT_NORMAL, 0);

  FMOD::Channel *channel = NULL;
  FMOD::Sound *sound = NULL;

  while(true)
    {
      system->createSound("music.waw", FMOD_DEFAULT, FMOD_DEFAULT ,&sound);

      FMOD_RESULT result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);

      system->update();
    }
  system->release();                                                                                                                                                                          
  system = NULL;
  delete (system);
  delete (sound);
  delete (channel);
  return (0);
}

And I am worried, because if I play my game long enough, I completely fill my memory... What should I do? What am I doing wrong?


Solution

  • You need to release the sound after you are done playing it:

    system->createSound("music.waw", FMOD_DEFAULT, FMOD_DEFAULT ,&sound);
    FMOD_RESULT result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
    system->update();
    sound->release();
    

    If you're planning to reuse the sound a lot, you might consider just loading it up once and keeping it memory.