Fixed Valgrind memory leak warnings, either by freeing memory on exit or

(in one case) by not allocating file handles dynamically. While this isn't
really necessary at the moment, it makes it easier to find the real memory
leaks, if there are any.

svn-id: r19775
This commit is contained in:
Torbjörn Andersson 2005-12-11 08:30:48 +00:00
parent 38c3df4393
commit 7cbb9af113
4 changed files with 31 additions and 28 deletions

View File

@ -43,7 +43,7 @@ namespace Sword2 {
static AudioStream *makeCLUStream(Common::File *fp, int size);
static AudioStream *getAudioStream(SoundFileHandle *fh, const char *base, int cd, uint32 id, uint32 *numSamples) {
if (!fh->file->isOpen()) {
if (!fh->file.isOpen()) {
struct {
const char *ext;
int mode;
@ -82,13 +82,13 @@ static AudioStream *getAudioStream(SoundFileHandle *fh, const char *base, int cd
if (soundMode == 0)
return NULL;
fh->file->open(filename);
fh->file.open(filename);
fh->fileType = soundMode;
if (!fh->file->isOpen()) {
if (!fh->file.isOpen()) {
warning("Very strange fopen error");
return NULL;
}
if (fh->fileSize != fh->file->size()) {
if (fh->fileSize != fh->file.size()) {
if (fh->idxTab) {
free(fh->idxTab);
fh->idxTab = NULL;
@ -99,19 +99,19 @@ static AudioStream *getAudioStream(SoundFileHandle *fh, const char *base, int cd
uint32 entrySize = (fh->fileType == kCLUMode) ? 2 : 3;
if (!fh->idxTab) {
fh->file->seek(0);
fh->idxLen = fh->file->readUint32LE();
fh->file->seek(entrySize * 4);
fh->file.seek(0);
fh->idxLen = fh->file.readUint32LE();
fh->file.seek(entrySize * 4);
fh->idxTab = (uint32*)malloc(fh->idxLen * 3 * sizeof(uint32));
for (uint32 cnt = 0; cnt < fh->idxLen; cnt++) {
fh->idxTab[cnt * 3 + 0] = fh->file->readUint32LE();
fh->idxTab[cnt * 3 + 1] = fh->file->readUint32LE();
fh->idxTab[cnt * 3 + 0] = fh->file.readUint32LE();
fh->idxTab[cnt * 3 + 1] = fh->file.readUint32LE();
if (fh->fileType == kCLUMode) {
fh->idxTab[cnt * 3 + 2] = fh->idxTab[cnt * 3 + 1];
fh->idxTab[cnt * 3 + 1]--;
} else
fh->idxTab[cnt * 3 + 2] = fh->file->readUint32LE();
fh->idxTab[cnt * 3 + 2] = fh->file.readUint32LE();
}
}
@ -123,26 +123,26 @@ static AudioStream *getAudioStream(SoundFileHandle *fh, const char *base, int cd
*numSamples = len;
if (!pos || !len) {
fh->file->close();
fh->file.close();
return NULL;
}
fh->file->seek(pos, SEEK_SET);
fh->file.seek(pos, SEEK_SET);
switch (fh->fileType) {
case kCLUMode:
return makeCLUStream(fh->file, enc_len);
return makeCLUStream(&fh->file, enc_len);
#ifdef USE_MAD
case kMP3Mode:
return makeMP3Stream(fh->file, enc_len);
return makeMP3Stream(&fh->file, enc_len);
#endif
#ifdef USE_VORBIS
case kVorbisMode:
return makeVorbisStream(fh->file, enc_len);
return makeVorbisStream(&fh->file, enc_len);
#endif
#ifdef USE_FLAC
case kFlacMode:
return makeFlacStream(fh->file, enc_len);
return makeFlacStream(&fh->file, enc_len);
#endif
default:
return NULL;
@ -461,8 +461,8 @@ int Sound::readBuffer(int16 *buffer, const int numSamples) {
}
for (i = 0; i < MAXMUS; i++) {
if (!inUse[i] && !_musicFile[i].inUse && _musicFile[i].file->isOpen())
_musicFile[i].file->close();
if (!inUse[i] && !_musicFile[i].inUse && _musicFile[i].file.isOpen())
_musicFile[i].file.close();
}
return numSamples;
@ -470,7 +470,7 @@ int Sound::readBuffer(int16 *buffer, const int numSamples) {
bool Sound::endOfData() const {
for (int i = 0; i < MAXMUS; i++) {
if (_musicFile[i].file->isOpen())
if (_musicFile[i].file.isOpen())
return false;
}
@ -727,13 +727,13 @@ uint32 Sound::preFetchCompSpeech(uint32 speechId, uint16 **buf) {
*buf = (uint16 *)malloc(bufferSize);
if (!*buf) {
delete input;
fh->file->close();
fh->file.close();
return 0;
}
uint32 readSamples = input->readBuffer((int16 *)*buf, numSamples);
fh->file->close();
fh->file.close();
delete input;
return 2 * readSamples;

View File

@ -189,6 +189,8 @@ ResourceManager::~ResourceManager() {
_vm->_memory->memFree(res->ptr);
res = res->next;
}
for (int i = 0; i < _totalClusters; i++)
free(_resFiles[i].entryTab);
free(_resList);
free(_resConvTable);
}

View File

@ -53,14 +53,12 @@ Sound::Sound(Sword2Engine *vm) {
for (i = 0; i < MAXMUS; i++) {
_music[i] = NULL;
_musicFile[i].file = new Common::File;
_musicFile[i].idxTab = NULL;
_musicFile[i].idxLen = 0;
_musicFile[i].fileSize = 0;
_musicFile[i].fileType = 0;
_musicFile[i].inUse = false;
_speechFile[i].file = new Common::File;
_speechFile[i].idxTab = NULL;
_speechFile[i].idxLen = 0;
_speechFile[i].fileSize = 0;
@ -96,10 +94,13 @@ Sound::~Sound() {
free(_mixBuffer);
for (int i = 0; i < MAXMUS; i++) {
if (_musicFile[i].file->isOpen())
_musicFile[i].file->close();
if (_speechFile[i].file->isOpen())
_speechFile[i].file->close();
if (_musicFile[i].file.isOpen())
_musicFile[i].file.close();
if (_speechFile[i].file.isOpen())
_speechFile[i].file.close();
free(_musicFile[i].idxTab);
free(_speechFile[i].idxTab);
}
}

View File

@ -109,7 +109,7 @@ public:
};
struct SoundFileHandle {
Common::File *file;
Common::File file;
uint32 *idxTab;
uint32 idxLen;
uint32 fileSize;