FULLPIPE: Improve memory safety and fix leaks in sound code

This commit is contained in:
Colin Snover 2017-11-12 10:16:34 -06:00 committed by Eugene Sandulenko
parent 8f1d76d261
commit 72a4292727
7 changed files with 40 additions and 61 deletions

View File

@ -345,7 +345,7 @@ int global_messageHandler2(ExCommand *cmd) {
SoundList *s = g_fp->_currSoundList1[snd]; SoundList *s = g_fp->_currSoundList1[snd];
int ms = s->getCount(); int ms = s->getCount();
for (int i = 0; i < ms; i++) { for (int i = 0; i < ms; i++) {
s->getSoundByIndex(i)->setPanAndVolumeByStaticAni(); s->getSoundByIndex(i).setPanAndVolumeByStaticAni();
} }
} }
} }

View File

@ -1504,11 +1504,11 @@ void ModalMainMenu::updateVolume() {
} }
} }
void ModalMainMenu::updateSoundVolume(Sound *snd) { void ModalMainMenu::updateSoundVolume(Sound &snd) {
if (!snd->_objectId) if (!snd._objectId)
return; return;
StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd->_objectId, -1); StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd._objectId, -1);
if (!ani) if (!ani)
return; return;
@ -1522,7 +1522,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
if (ani->_oy <= _screct.bottom) { if (ani->_oy <= _screct.bottom) {
if (ani->_oy >= _screct.top) { if (ani->_oy >= _screct.top) {
snd->setPanAndVolume(g_fp->_sfxVolume, 0); snd.setPanAndVolume(g_fp->_sfxVolume, 0);
return; return;
} }
@ -1534,7 +1534,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
par = 0; par = 0;
if (dx > 800) { if (dx > 800) {
snd->setPanAndVolume(-3500, 0); snd.setPanAndVolume(-3500, 0);
return; return;
} }
@ -1545,7 +1545,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
int dx = ani->_ox - _screct.right; int dx = ani->_ox - _screct.right;
if (dx > 800) { if (dx > 800) {
snd->setPanAndVolume(-3500, 0); snd.setPanAndVolume(-3500, 0);
return; return;
} }
@ -1557,7 +1557,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
int32 pp = b * a; int32 pp = b * a;
snd->setPanAndVolume(pan + pp / 800, par); snd.setPanAndVolume(pan + pp / 800, par);
return; return;
} }
@ -1570,9 +1570,9 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
if (p > g_fp->_sfxVolume) if (p > g_fp->_sfxVolume)
p = g_fp->_sfxVolume; p = g_fp->_sfxVolume;
snd->setPanAndVolume(p, dx * (-3500) / 800); snd.setPanAndVolume(p, dx * (-3500) / 800);
} else { } else {
snd->setPanAndVolume(-3500, 0); snd.setPanAndVolume(-3500, 0);
} }
} }

View File

@ -228,7 +228,7 @@ private:
void enableDebugMenu(char c); void enableDebugMenu(char c);
int checkHover(Common::Point &point); int checkHover(Common::Point &point);
void updateVolume(); void updateVolume();
void updateSoundVolume(Sound *snd); void updateSoundVolume(Sound &snd);
void updateSliderPos(); void updateSliderPos();
bool isOverArea(PictureObject *obj, Common::Point *point); bool isOverArea(PictureObject *obj, Common::Point *point);
}; };

View File

@ -342,7 +342,7 @@ void Scene::setPictureObjectsFlag4() {
void Scene::stopAllSounds() { void Scene::stopAllSounds() {
for (int i = 0; i < _soundList->getCount(); i++) for (int i = 0; i < _soundList->getCount(); i++)
_soundList->getSoundByIndex(i)->stop(); _soundList->getSoundByIndex(i).stop();
} }
PictureObject *Scene::getPictureObjectById(int objId, int flags) { PictureObject *Scene::getPictureObjectById(int objId, int flags) {

View File

@ -577,7 +577,7 @@ bool FullpipeEngine::sceneSwitcher(EntranceInfo *entrance) {
_currSoundList1[1] = scene->_soundList; _currSoundList1[1] = scene->_soundList;
for (int i = 0; i < scene->_soundList->getCount(); i++) { for (int i = 0; i < scene->_soundList->getCount(); i++) {
scene->_soundList->getSoundByIndex(i)->updateVolume(); scene->_soundList->getSoundByIndex(i).updateVolume();
} }
} else { } else {
_currSoundListCount = 1; _currSoundListCount = 1;

View File

@ -38,39 +38,23 @@
namespace Fullpipe { 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) { bool SoundList::load(MfcArchive &file, const Common::String &fname) {
debugC(5, kDebugLoading, "SoundList::load()"); debugC(5, kDebugLoading, "SoundList::load()");
_soundItemsCount = file.readUint32LE(); uint32 count = file.readUint32LE();
_soundItems = (Sound **)calloc(_soundItemsCount, sizeof(Sound *)); _soundItems.resize(count);
if (!fname.empty()) { if (!fname.empty()) {
_libHandle = makeNGIArchive(fname); _libHandle.reset(makeNGIArchive(fname));
} else { } else {
_libHandle = 0; _libHandle.reset();
} }
for (int i = 0; i < _soundItemsCount; i++) { for (uint i = 0; i < count; i++) {
Sound *snd = new Sound(); _soundItems[i].load(file, _libHandle.get());
_soundItems[i] = snd;
snd->load(file, _libHandle);
} }
return true; return true;
} }
bool SoundList::loadFile(const Common::String &fname, const Common::String &libname) { 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) { Sound *SoundList::getSoundItemById(int id) {
if (_soundItemsCount == 0) { for (uint i = 0; i < _soundItems.size(); ++i) {
return _soundItems[0]->getId() != id ? 0 : _soundItems[0]; if (_soundItems[i].getId() == id)
return &_soundItems[i];
} }
return nullptr;
for (int i = 0; i < _soundItemsCount; i++) {
if (_soundItems[i]->getId() == id)
return _soundItems[i];
}
return NULL;
} }
Sound::Sound() { Sound::Sound() :
_id = 0; _id(0),
_directSoundBuffer = 0; _directSoundBuffer(0),
_soundData = 0; _directSoundBuffers(),
_objectId = 0; _soundData(nullptr),
memset(_directSoundBuffers, 0, sizeof(_directSoundBuffers)); _handle(new Audio::SoundHandle()),
_volume = 100; _volume(100),
_handle = new Audio::SoundHandle(); _objectId(0) {}
}
Sound::~Sound() { Sound::~Sound() {
freeSound(); freeSound();
@ -389,7 +368,7 @@ void FullpipeEngine::playOggSound(const Common::String &trackName, Audio::SoundH
void FullpipeEngine::stopAllSounds() { void FullpipeEngine::stopAllSounds() {
for (int i = 0; i < _currSoundListCount; i++) for (int i = 0; i < _currSoundListCount; i++)
for (int j = 0; j < _currSoundList1[i]->getCount(); j++) { 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 i = 0; i < _currSoundListCount; i++)
for (int j = 0; j < _currSoundList1[i]->getCount(); j++) { for (int j = 0; j < _currSoundList1[i]->getCount(); j++) {
_currSoundList1[i]->getSoundByIndex(j)->setPanAndVolume(_sfxVolume, 0); _currSoundList1[i]->getSoundByIndex(j).setPanAndVolume(_sfxVolume, 0);
} }
} }

View File

@ -23,6 +23,9 @@
#ifndef FULLPIPE_SOUND_H #ifndef FULLPIPE_SOUND_H
#define FULLPIPE_SOUND_H #define FULLPIPE_SOUND_H
#include "common/array.h"
#include "common/ptr.h"
namespace Audio { namespace Audio {
class SoundHandle; class SoundHandle;
} }
@ -60,19 +63,16 @@ public:
}; };
class SoundList : public CObject { class SoundList : public CObject {
Sound **_soundItems; Common::Array<Sound> _soundItems;
int _soundItemsCount; Common::ScopedPtr<NGIArchive> _libHandle;
NGIArchive *_libHandle;
public: public:
SoundList();
~SoundList();
virtual bool load(MfcArchive &file, const Common::String &fname); virtual bool load(MfcArchive &file, const Common::String &fname);
virtual bool load(MfcArchive &file) { assert(0); return false; } // Disable base class virtual bool load(MfcArchive &file) { assert(0); return false; } // Disable base class
bool loadFile(const Common::String &fname, const Common::String &libname); bool loadFile(const Common::String &fname, const Common::String &libname);
int getCount() { return _soundItemsCount; } int getCount() { return _soundItems.size(); }
Sound *getSoundByIndex(int idx) { return _soundItems[idx]; } Sound &getSoundByIndex(int idx) { return _soundItems[idx]; }
Sound *getSoundItemById(int id); Sound *getSoundItemById(int id);
}; };