From 25ad71a113360179217e5848683d18a729e73aa9 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 30 Nov 2018 17:07:42 +0100 Subject: [PATCH] - Changed a few allocations into usin TArrays S_SoundCurve in particular looked like a candidate for leaking memory. --- src/s_sound.cpp | 68 +++++++++++++++++------------------------- src/s_sound.h | 4 +-- src/sc_man.cpp | 12 +++----- src/sound/oalsound.cpp | 4 +-- src/v_font.cpp | 18 +++++------ 5 files changed, 43 insertions(+), 63 deletions(-) diff --git a/src/s_sound.cpp b/src/s_sound.cpp index be12d3693..8a10d71b7 100644 --- a/src/s_sound.cpp +++ b/src/s_sound.cpp @@ -150,8 +150,7 @@ FSoundChan *Channels; FSoundChan *FreeChannels; FRolloffInfo S_Rolloff; -uint8_t *S_SoundCurve; -int S_SoundCurveSize; +TArray S_SoundCurve; FBoolCVar noisedebug ("noise", false, 0); // [RH] Print sound debugging info? CUSTOM_CVAR (Int, snd_channels, 128, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) // number of channels available @@ -311,20 +310,16 @@ void S_Init () atterm (S_Shutdown); - // remove old data (S_Init can be called multiple times!) - if (S_SoundCurve != NULL) - { - delete[] S_SoundCurve; - S_SoundCurve = NULL; - } - // Heretic and Hexen have sound curve lookup tables. Doom does not. curvelump = Wads.CheckNumForName ("SNDCURVE"); if (curvelump >= 0) { - S_SoundCurveSize = Wads.LumpLength (curvelump); - S_SoundCurve = new uint8_t[S_SoundCurveSize]; - Wads.ReadLump(curvelump, S_SoundCurve); + S_SoundCurve.Resize(Wads.LumpLength (curvelump)); + Wads.ReadLump(curvelump, S_SoundCurve.Data()); + } + else + { + S_SoundCurve.Clear(); } // Free all channels for use. @@ -376,11 +371,6 @@ void S_Shutdown () } FreeChannels = NULL; - if (S_SoundCurve != NULL) - { - delete[] S_SoundCurve; - S_SoundCurve = NULL; - } if (PlayList != NULL) { delete PlayList; @@ -1486,34 +1476,32 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer) if (size > 0) { auto wlump = Wads.OpenLumpReader(sfx->lumpnum); - uint8_t *sfxdata = new uint8_t[size]; - wlump.Read(sfxdata, size); - int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]); + auto sfxdata = wlump.Read(size); + int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]); std::pair snd; // If the sound is voc, use the custom loader. - if (strncmp ((const char *)sfxdata, "Creative Voice File", 19) == 0) + if (strncmp ((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0) { - snd = GSnd->LoadSoundVoc(sfxdata, size); + snd = GSnd->LoadSoundVoc(sfxdata.Data(), size); } // If the sound is raw, just load it as such. else if (sfx->bLoadRAW) { - snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart); + snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart); } // Otherwise, try the sound as DMX format. - else if (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8) + else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8) { - int frequency = LittleShort(((uint16_t *)sfxdata)[1]); + int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]); if (frequency == 0) frequency = 11025; - snd = GSnd->LoadSoundRaw(sfxdata+8, dmxlen, frequency, 1, 8, sfx->LoopStart); + snd = GSnd->LoadSoundRaw(sfxdata.Data()+8, dmxlen, frequency, 1, 8, sfx->LoopStart); } // If that fails, let the sound system try and figure it out. else { - snd = GSnd->LoadSound(sfxdata, size, false, pBuffer); + snd = GSnd->LoadSound(sfxdata.Data(), size, false, pBuffer); } - delete[] sfxdata; sfx->data = snd.first; if(snd.second) @@ -1554,33 +1542,31 @@ static void S_LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer) if (size <= 0) return; auto wlump = Wads.OpenLumpReader(sfx->lumpnum); - uint8_t *sfxdata = new uint8_t[size]; - wlump.Read(sfxdata, size); - int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]); + auto sfxdata = wlump.Read(size); + int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]); // If the sound is voc, use the custom loader. - if (strncmp((const char *)sfxdata, "Creative Voice File", 19) == 0) + if (strncmp((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0) { - snd = GSnd->LoadSoundVoc(sfxdata, size, true); + snd = GSnd->LoadSoundVoc(sfxdata.Data(), size, true); } // If the sound is raw, just load it as such. else if (sfx->bLoadRAW) { - snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart, true); + snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart, true); } // Otherwise, try the sound as DMX format. - else if (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8) + else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8) { - int frequency = LittleShort(((uint16_t *)sfxdata)[1]); + int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]); if (frequency == 0) frequency = 11025; - snd = GSnd->LoadSoundRaw(sfxdata + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true); + snd = GSnd->LoadSoundRaw(sfxdata.Data() + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true); } // If that fails, let the sound system try and figure it out. else { - snd = GSnd->LoadSound(sfxdata, size, true, pBuffer); + snd = GSnd->LoadSound(sfxdata.Data(), size, true, pBuffer); } - delete[] sfxdata; } sfx->data3d = snd.first; @@ -2256,9 +2242,9 @@ float S_GetRolloff(FRolloffInfo *rolloff, float distance, bool logarithmic) } float volume = (rolloff->MaxDistance - distance) / (rolloff->MaxDistance - rolloff->MinDistance); - if (rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve != NULL) + if (rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve.Size() > 0) { - volume = S_SoundCurve[int(S_SoundCurveSize * (1 - volume))] / 127.f; + volume = S_SoundCurve[int(S_SoundCurve.Size() * (1 - volume))] / 127.f; } if (logarithmic) { diff --git a/src/s_sound.h b/src/s_sound.h index 1d42cb2ae..5ff5110e7 100644 --- a/src/s_sound.h +++ b/src/s_sound.h @@ -165,8 +165,8 @@ public: }; extern FRolloffInfo S_Rolloff; -extern uint8_t *S_SoundCurve; -extern int S_SoundCurveSize; +extern TArray S_SoundCurve; + // Information about one playing sound. struct sector_t; diff --git a/src/sc_man.cpp b/src/sc_man.cpp index 44fbdb42c..7eb6b3c3a 100644 --- a/src/sc_man.cpp +++ b/src/sc_man.cpp @@ -249,14 +249,10 @@ bool FScanner::OpenFile (const char *name) FileReader fr; if (!fr.OpenFile(name)) return false; auto filesize = fr.GetLength(); - auto filebuf = new uint8_t[filesize]; - if (fr.Read(filebuf, filesize) != filesize) - { - delete[] filebuf; - return false; - } - ScriptBuffer = FString((const char *)filebuf, filesize); - delete[] filebuf; + auto filebuff = fr.Read(); + if (filebuff.Size() == 0 && filesize > 0) return false; + + ScriptBuffer = FString((const char *)filebuff.Data(), filesize); ScriptName = name; // This is used for error messages so the full file name is preferable LumpNum = -1; PrepareScript (); diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 4b494ed11..ed3098d9f 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -688,8 +688,8 @@ static float GetRolloff(const FRolloffInfo *rolloff, float distance) if(rolloff->RolloffType == ROLLOFF_Linear) return volume; - if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve != NULL) - return S_SoundCurve[int(S_SoundCurveSize * (1.f - volume))] / 127.f; + if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve.Size() > 0) + return S_SoundCurve[int(S_SoundCurve.Size() * (1.f - volume))] / 127.f; return (powf(10.f, volume) - 1.f) / 9.f; } diff --git a/src/v_font.cpp b/src/v_font.cpp index 4678c56b9..3a589e741 100644 --- a/src/v_font.cpp +++ b/src/v_font.cpp @@ -357,7 +357,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, int i; FTextureID lump; char buffer[12]; - FTexture **charlumps; + TArray charLumps; int maxyoffs; bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false; bool stcfn121 = false; @@ -365,7 +365,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, noTranslate = notranslate; Lump = fdlump; Chars = new CharData[count]; - charlumps = new FTexture *[count]; + charLumps.Resize(count); PatchRemap = new uint8_t[256]; FirstChar = first; LastChar = first + count - 1; @@ -381,7 +381,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, for (i = 0; i < count; i++) { - charlumps[i] = NULL; + charLumps[i] = NULL; mysnprintf (buffer, countof(buffer), nametemplate, i + start); lump = TexMan.CheckForTexture(buffer, ETextureType::MiscPatch); @@ -395,7 +395,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, !TexMan.CheckForTexture("STCFN122", ETextureType::MiscPatch).isValid()) { // insert the incorrectly named '|' graphic in its correct position. - if (count > 124-start) charlumps[124-start] = TexMan[lump]; + if (count > 124-start) charLumps[124-start] = TexMan[lump]; lump.SetInvalid(); stcfn121 = true; } @@ -408,7 +408,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, { // set the lump here only if it represents a valid texture if (i != 124-start || !stcfn121) - charlumps[i] = pic; + charLumps[i] = pic; int height = pic->GetScaledHeight(); int yoffs = pic->GetScaledTopOffset(0); @@ -425,10 +425,10 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, } } - if (charlumps[i] != NULL) + if (charLumps[i] != nullptr) { - if (!noTranslate) Chars[i].Pic = new FFontChar1 (charlumps[i]); - else Chars[i].Pic = charlumps[i]; + if (!noTranslate) Chars[i].Pic = new FFontChar1 (charLumps[i]); + else Chars[i].Pic = charLumps[i]; Chars[i].XMove = Chars[i].Pic->GetScaledWidth(); } else @@ -454,8 +454,6 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, FixXMoves(); if (!noTranslate) LoadTranslations(); - - delete[] charlumps; } //==========================================================================