user
user

Reputation: 301

FMOD - Memory usage keeps increasing when calling createStream for multiple times

Currently, I am trying to design a sound player by using FMOD. I want to achieve the goal just uses one Sound pointer but end up memory usage keep increasing when calling createStream for multiple times. Is that anyway to improve it without using multiple Sound pointer?

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

Upvotes: 2

Views: 551

Answers (1)

Preet Kukreti
Preet Kukreti

Reputation: 8617

One minor memory leak is that you have m_trackLength being new-ed but not delete-d. There might be something else that is contributing though.

Upvotes: 1

Related Questions