简体   繁体   English

FMOD在播放声音时泄漏

[英]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? 我究竟做错了什么?

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. 如果您打算大量重复使用声音,则可以考虑将其加载一次并保留其内存。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM