diff options
author | Colin Snover | 2017-11-12 10:16:34 -0600 |
---|---|---|
committer | Eugene Sandulenko | 2017-11-18 22:35:12 +0100 |
commit | 72a42927279a8b740cd0bb5a8053def4a465f2dd (patch) | |
tree | 81ad3321e6e6898c19d4d26c1c3ae64bcbc536c4 | |
parent | 8f1d76d2616489924f472815f91107cd46d479de (diff) | |
download | scummvm-rg350-72a42927279a8b740cd0bb5a8053def4a465f2dd.tar.gz scummvm-rg350-72a42927279a8b740cd0bb5a8053def4a465f2dd.tar.bz2 scummvm-rg350-72a42927279a8b740cd0bb5a8053def4a465f2dd.zip |
FULLPIPE: Improve memory safety and fix leaks in sound code
-rw-r--r-- | engines/fullpipe/messagehandlers.cpp | 2 | ||||
-rw-r--r-- | engines/fullpipe/modal.cpp | 18 | ||||
-rw-r--r-- | engines/fullpipe/modal.h | 2 | ||||
-rw-r--r-- | engines/fullpipe/scene.cpp | 2 | ||||
-rw-r--r-- | engines/fullpipe/scenes.cpp | 2 | ||||
-rw-r--r-- | engines/fullpipe/sound.cpp | 61 | ||||
-rw-r--r-- | engines/fullpipe/sound.h | 14 |
7 files changed, 40 insertions, 61 deletions
diff --git a/engines/fullpipe/messagehandlers.cpp b/engines/fullpipe/messagehandlers.cpp index bd75e01232..a172184798 100644 --- a/engines/fullpipe/messagehandlers.cpp +++ b/engines/fullpipe/messagehandlers.cpp @@ -345,7 +345,7 @@ int global_messageHandler2(ExCommand *cmd) { SoundList *s = g_fp->_currSoundList1[snd]; int ms = s->getCount(); for (int i = 0; i < ms; i++) { - s->getSoundByIndex(i)->setPanAndVolumeByStaticAni(); + s->getSoundByIndex(i).setPanAndVolumeByStaticAni(); } } } diff --git a/engines/fullpipe/modal.cpp b/engines/fullpipe/modal.cpp index 3faa035c60..6589ba6991 100644 --- a/engines/fullpipe/modal.cpp +++ b/engines/fullpipe/modal.cpp @@ -1504,11 +1504,11 @@ void ModalMainMenu::updateVolume() { } } -void ModalMainMenu::updateSoundVolume(Sound *snd) { - if (!snd->_objectId) +void ModalMainMenu::updateSoundVolume(Sound &snd) { + if (!snd._objectId) return; - StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd->_objectId, -1); + StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd._objectId, -1); if (!ani) return; @@ -1522,7 +1522,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) { if (ani->_oy <= _screct.bottom) { if (ani->_oy >= _screct.top) { - snd->setPanAndVolume(g_fp->_sfxVolume, 0); + snd.setPanAndVolume(g_fp->_sfxVolume, 0); return; } @@ -1534,7 +1534,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) { par = 0; if (dx > 800) { - snd->setPanAndVolume(-3500, 0); + snd.setPanAndVolume(-3500, 0); return; } @@ -1545,7 +1545,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) { int dx = ani->_ox - _screct.right; if (dx > 800) { - snd->setPanAndVolume(-3500, 0); + snd.setPanAndVolume(-3500, 0); return; } @@ -1557,7 +1557,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) { int32 pp = b * a; - snd->setPanAndVolume(pan + pp / 800, par); + snd.setPanAndVolume(pan + pp / 800, par); return; } @@ -1570,9 +1570,9 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) { if (p > g_fp->_sfxVolume) p = g_fp->_sfxVolume; - snd->setPanAndVolume(p, dx * (-3500) / 800); + snd.setPanAndVolume(p, dx * (-3500) / 800); } else { - snd->setPanAndVolume(-3500, 0); + snd.setPanAndVolume(-3500, 0); } } diff --git a/engines/fullpipe/modal.h b/engines/fullpipe/modal.h index ba8880827d..6cd4581b39 100644 --- a/engines/fullpipe/modal.h +++ b/engines/fullpipe/modal.h @@ -228,7 +228,7 @@ private: void enableDebugMenu(char c); int checkHover(Common::Point &point); void updateVolume(); - void updateSoundVolume(Sound *snd); + void updateSoundVolume(Sound &snd); void updateSliderPos(); bool isOverArea(PictureObject *obj, Common::Point *point); }; diff --git a/engines/fullpipe/scene.cpp b/engines/fullpipe/scene.cpp index 74a824f018..3d39683431 100644 --- a/engines/fullpipe/scene.cpp +++ b/engines/fullpipe/scene.cpp @@ -342,7 +342,7 @@ void Scene::setPictureObjectsFlag4() { void Scene::stopAllSounds() { for (int i = 0; i < _soundList->getCount(); i++) - _soundList->getSoundByIndex(i)->stop(); + _soundList->getSoundByIndex(i).stop(); } PictureObject *Scene::getPictureObjectById(int objId, int flags) { diff --git a/engines/fullpipe/scenes.cpp b/engines/fullpipe/scenes.cpp index bb141d98c4..7825a1eb2d 100644 --- a/engines/fullpipe/scenes.cpp +++ b/engines/fullpipe/scenes.cpp @@ -577,7 +577,7 @@ bool FullpipeEngine::sceneSwitcher(EntranceInfo *entrance) { _currSoundList1[1] = scene->_soundList; for (int i = 0; i < scene->_soundList->getCount(); i++) { - scene->_soundList->getSoundByIndex(i)->updateVolume(); + scene->_soundList->getSoundByIndex(i).updateVolume(); } } else { _currSoundListCount = 1; diff --git a/engines/fullpipe/sound.cpp b/engines/fullpipe/sound.cpp index 4ab6520c33..44dbe56cc0 100644 --- a/engines/fullpipe/sound.cpp +++ b/engines/fullpipe/sound.cpp @@ -38,39 +38,23 @@ namespace Fullpipe { -SoundList::SoundList() { - _soundItems = 0; - _soundItemsCount = 0; - _libHandle = 0; -} - -SoundList::~SoundList() { - for (int i = 0; i < _soundItemsCount; i++) - delete _soundItems[i]; - free(_soundItems); -} - bool SoundList::load(MfcArchive &file, const Common::String &fname) { debugC(5, kDebugLoading, "SoundList::load()"); - _soundItemsCount = file.readUint32LE(); - _soundItems = (Sound **)calloc(_soundItemsCount, sizeof(Sound *)); + uint32 count = file.readUint32LE(); + _soundItems.resize(count); if (!fname.empty()) { - _libHandle = makeNGIArchive(fname); + _libHandle.reset(makeNGIArchive(fname)); } else { - _libHandle = 0; + _libHandle.reset(); } - for (int i = 0; i < _soundItemsCount; i++) { - Sound *snd = new Sound(); - - _soundItems[i] = snd; - snd->load(file, _libHandle); + for (uint i = 0; i < count; i++) { + _soundItems[i].load(file, _libHandle.get()); } return true; - } bool SoundList::loadFile(const Common::String &fname, const Common::String &libname) { @@ -85,26 +69,21 @@ bool SoundList::loadFile(const Common::String &fname, const Common::String &libn } Sound *SoundList::getSoundItemById(int id) { - if (_soundItemsCount == 0) { - return _soundItems[0]->getId() != id ? 0 : _soundItems[0]; - } - - for (int i = 0; i < _soundItemsCount; i++) { - if (_soundItems[i]->getId() == id) - return _soundItems[i]; + for (uint i = 0; i < _soundItems.size(); ++i) { + if (_soundItems[i].getId() == id) + return &_soundItems[i]; } - return NULL; + return nullptr; } -Sound::Sound() { - _id = 0; - _directSoundBuffer = 0; - _soundData = 0; - _objectId = 0; - memset(_directSoundBuffers, 0, sizeof(_directSoundBuffers)); - _volume = 100; - _handle = new Audio::SoundHandle(); -} +Sound::Sound() : + _id(0), + _directSoundBuffer(0), + _directSoundBuffers(), + _soundData(nullptr), + _handle(new Audio::SoundHandle()), + _volume(100), + _objectId(0) {} Sound::~Sound() { freeSound(); @@ -389,7 +368,7 @@ void FullpipeEngine::playOggSound(const Common::String &trackName, Audio::SoundH void FullpipeEngine::stopAllSounds() { for (int i = 0; i < _currSoundListCount; i++) for (int j = 0; j < _currSoundList1[i]->getCount(); j++) { - _currSoundList1[i]->getSoundByIndex(j)->stop(); + _currSoundList1[i]->getSoundByIndex(j).stop(); } } @@ -542,7 +521,7 @@ void FullpipeEngine::updateSoundVolume() { for (int i = 0; i < _currSoundListCount; i++) for (int j = 0; j < _currSoundList1[i]->getCount(); j++) { - _currSoundList1[i]->getSoundByIndex(j)->setPanAndVolume(_sfxVolume, 0); + _currSoundList1[i]->getSoundByIndex(j).setPanAndVolume(_sfxVolume, 0); } } diff --git a/engines/fullpipe/sound.h b/engines/fullpipe/sound.h index bccf2a781c..60584160e5 100644 --- a/engines/fullpipe/sound.h +++ b/engines/fullpipe/sound.h @@ -23,6 +23,9 @@ #ifndef FULLPIPE_SOUND_H #define FULLPIPE_SOUND_H +#include "common/array.h" +#include "common/ptr.h" + namespace Audio { class SoundHandle; } @@ -60,19 +63,16 @@ public: }; class SoundList : public CObject { - Sound **_soundItems; - int _soundItemsCount; - NGIArchive *_libHandle; + Common::Array<Sound> _soundItems; + Common::ScopedPtr<NGIArchive> _libHandle; public: - SoundList(); - ~SoundList(); virtual bool load(MfcArchive &file, const Common::String &fname); virtual bool load(MfcArchive &file) { assert(0); return false; } // Disable base class bool loadFile(const Common::String &fname, const Common::String &libname); - int getCount() { return _soundItemsCount; } - Sound *getSoundByIndex(int idx) { return _soundItems[idx]; } + int getCount() { return _soundItems.size(); } + Sound &getSoundByIndex(int idx) { return _soundItems[idx]; } Sound *getSoundItemById(int id); }; |