0

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?

Nickel
  • 13
  • 4

2 Answers2

0

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.

jaket
  • 9,140
  • 2
  • 25
  • 44
0

here is some suggestions to avoid memory leak

result = pitch_shift_DSP->release();// if you have DSP like me.
ERRCHECK(result);
result = sound->release();
ERRCHECK(result);
result = system->close();
ERRCHECK(result);
result = system->release();
ERRCHECK(result);
Matthew
  • 11
  • 4