c++ - FMOD - 多次调用 createStream 时内存使用量不断增加

标签 c++ memory stream allocation fmod

目前,我正在尝试使用 FMOD 设计一个声音播放器。我想实现只使用一个 Sound 指针的目标,但最终在多次调用 createStream 时内存使用量不断增加。无论如何不使用多个声音指针来改进它吗?

声音类.h

#ifndef _SOUNDCLASS_H_
#define _SOUNDCLASS_H_


//////////////
// INCLUDES //
//////////////
#include "FMOD/fmod.hpp"
using namespace FMOD;


////////////////////////////////////////////////////////////////////////////////
// Class name: SoundClass
////////////////////////////////////////////////////////////////////////////////
class SoundClass
{
public:
    SoundClass();
    ~SoundClass();
    bool Initialize();
    float Load( const int& trackNo );
    void Play();
    void Stop();
    void Pause();
    void Resume();
    void Next( int& trackNo, float& totalTrackLength );
    void Previous( int& trackNo, float& totalTrackLength );

private:
    bool m_isFirstRun;
    bool m_isLoad;
    bool m_isPlay;
    int  m_currentTrackNo;
    unsigned int* m_trackLength;

    FMOD_RESULT result;
    System* fmodSystem;
    Channel* fmodChannel;
    Sound* track;
};

#endif

声音类.cpp

#include "SoundClass.h"

SoundClass::SoundClass()
{
    fmodSystem          = 0;
    track               = 0;
    fmodChannel         = 0;
    m_isFirstRun        = true;
    m_isLoad            = true;
    m_isPlay            = true;
    m_currentTrackNo    = 0;
    m_trackLength       = new unsigned int;
}


SoundClass::~SoundClass()
{
    /*
    Cleaning up...
    */
    track->release();
    fmodSystem->release();
}


bool SoundClass::Initialize()
{
    /*
    Setup FMOD for your application.
    */

    //  Create a FMOD system.
    result = System_Create( &fmodSystem );

    //  Similar to HRESULT, FMOD use FMOD_RESULT to keep track of the execution result.
    if ( result != FMOD_OK )
    {
        return false;
    }

    //  Alternately, you can check for the availability of the system.
    if ( !fmodSystem )
    {
        return false;
    }

    //  Initialize FMOD.
    result = fmodSystem->init( 100, FMOD_INIT_NORMAL, 0 );

    if ( result != FMOD_OK )
    {
        return false;
    }

    return true;
 }


float SoundClass::Load( const int& trackNo )
{
    /*
     Load a sound file.
    */

    //  FMOD sound source.
    switch ( trackNo )
    {
    case 0:
        fmodSystem->createStream( "resources/audio/01.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 1:
        fmodSystem->createStream( "resources/audio/02.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 2:
        fmodSystem->createStream( "resources/audio/03.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 3:
        fmodSystem->createStream( "resources/audio/04.ogg", FMOD_DEFAULT, 0, &track );
        break;

   case 4:
    fmodSystem->createStream( "resources/audio/05.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 5:
        fmodSystem->createStream( "resources/audio/06.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 6:
        fmodSystem->createStream( "resources/audio/07.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 7:
        fmodSystem->createStream( "resources/audio/08.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 8:
        fmodSystem->createStream( "resources/audio/09.ogg", FMOD_DEFAULT, 0, &track );
        break;

    case 9:
        fmodSystem->createStream( "resources/audio/10.ogg", FMOD_DEFAULT, 0, &track );
        break;
}

    m_currentTrackNo = trackNo;

    track->getLength( m_trackLength, FMOD_TIMEUNIT_MS );
    return (float)*m_trackLength / 1000;
}


void SoundClass::Play( )
{
    //  Play sound on a channel
    fmodSystem->playSound( FMOD_CHANNEL_REUSE, track, false, &fmodChannel );
}


void SoundClass::Stop()
{
    fmodChannel->stop();
}


void SoundClass::Pause()
{
    fmodChannel->setPaused( true );
}


void SoundClass::Resume()
{
    fmodChannel->setPaused( false );
}


void SoundClass::Next( int& trackNo, float& totalTrackLength )
{
    if ( m_currentTrackNo != 9 )
    {
        totalTrackLength = Load( m_currentTrackNo + 1 );
       trackNo = m_currentTrackNo;
       Play();
    }
}


void SoundClass::Previous( int& trackNo, float& totalTrackLength )
{
    if ( m_currentTrackNo != 0 )
    {
        totalTrackLength = Load( m_currentTrackNo - 1 );
        trackNo = m_currentTrackNo;
        Play();
    }
}  

最佳答案

一个小的内存泄漏是您有 m_trackLengthnew-ed 而不是 delete-d。不过,可能还有其他因素在起作用。

关于c++ - FMOD - 多次调用 createStream 时内存使用量不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345412/

相关文章:

memory - 如何检测或探测或扫描可用/可访问的物理内存?

c# - TcpClient 收到数据时的事件

c++ - 有效地将一个标准流复制到另一个标准流

c++ - (C++) 在读取文件时如何使用带整数的 getline()?

python - 将数据从一个 .exe 传递到另一个

python - 找到三个 'connected' 矩阵的最大最小值的最快方法

stream - 我如何在 Rust 中使用 `flatmap` 流?

c++ - 在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?

c++ - 在从不再加载的动态库实例化的对象上使用在主代码库中定义的模板类方法时出现段错误

memory - Unity 分析我的脚本的内存使用情况