2013-10-13 76 views
2

目前,我試圖通過使用FMOD設計一款聲音播放器。我想實現這個目標只需要使用一個Sound指針,但是在多次調用createStream時,內存使用量會持續增加。無論如何要改善它,而不使用多個聲音指針?FMOD - 多次調用createStream時內存使用量不斷增加

SoundClass.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 

SoundClass.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(); 
    } 
} 
+0

請發佈一個重現問題的最小例子。 –

回答

1

一個次要內存泄漏是,你必須m_trackLengthnew -ed但不delete -d。可能還有其他的東西是有貢獻的。

+0

有人想知道爲什麼'm_trackLength'是一個指針:/ –

相關問題