- Changed a few allocations into usin TArrays

S_SoundCurve in particular looked like a candidate for leaking memory.
This commit is contained in:
Christoph Oelckers 2018-11-30 17:07:42 +01:00
parent 024870ba11
commit 25ad71a113
5 changed files with 43 additions and 63 deletions

View file

@ -150,8 +150,7 @@ FSoundChan *Channels;
FSoundChan *FreeChannels; FSoundChan *FreeChannels;
FRolloffInfo S_Rolloff; FRolloffInfo S_Rolloff;
uint8_t *S_SoundCurve; TArray<uint8_t> S_SoundCurve;
int S_SoundCurveSize;
FBoolCVar noisedebug ("noise", false, 0); // [RH] Print sound debugging info? FBoolCVar noisedebug ("noise", false, 0); // [RH] Print sound debugging info?
CUSTOM_CVAR (Int, snd_channels, 128, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) // number of channels available CUSTOM_CVAR (Int, snd_channels, 128, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) // number of channels available
@ -311,20 +310,16 @@ void S_Init ()
atterm (S_Shutdown); 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. // Heretic and Hexen have sound curve lookup tables. Doom does not.
curvelump = Wads.CheckNumForName ("SNDCURVE"); curvelump = Wads.CheckNumForName ("SNDCURVE");
if (curvelump >= 0) if (curvelump >= 0)
{ {
S_SoundCurveSize = Wads.LumpLength (curvelump); S_SoundCurve.Resize(Wads.LumpLength (curvelump));
S_SoundCurve = new uint8_t[S_SoundCurveSize]; Wads.ReadLump(curvelump, S_SoundCurve.Data());
Wads.ReadLump(curvelump, S_SoundCurve); }
else
{
S_SoundCurve.Clear();
} }
// Free all channels for use. // Free all channels for use.
@ -376,11 +371,6 @@ void S_Shutdown ()
} }
FreeChannels = NULL; FreeChannels = NULL;
if (S_SoundCurve != NULL)
{
delete[] S_SoundCurve;
S_SoundCurve = NULL;
}
if (PlayList != NULL) if (PlayList != NULL)
{ {
delete PlayList; delete PlayList;
@ -1486,34 +1476,32 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
if (size > 0) if (size > 0)
{ {
auto wlump = Wads.OpenLumpReader(sfx->lumpnum); auto wlump = Wads.OpenLumpReader(sfx->lumpnum);
uint8_t *sfxdata = new uint8_t[size]; auto sfxdata = wlump.Read(size);
wlump.Read(sfxdata, size); int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]);
std::pair<SoundHandle,bool> snd; std::pair<SoundHandle,bool> snd;
// If the sound is voc, use the custom loader. // 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. // If the sound is raw, just load it as such.
else if (sfx->bLoadRAW) 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. // 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; 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. // If that fails, let the sound system try and figure it out.
else else
{ {
snd = GSnd->LoadSound(sfxdata, size, false, pBuffer); snd = GSnd->LoadSound(sfxdata.Data(), size, false, pBuffer);
} }
delete[] sfxdata;
sfx->data = snd.first; sfx->data = snd.first;
if(snd.second) if(snd.second)
@ -1554,33 +1542,31 @@ static void S_LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
if (size <= 0) return; if (size <= 0) return;
auto wlump = Wads.OpenLumpReader(sfx->lumpnum); auto wlump = Wads.OpenLumpReader(sfx->lumpnum);
uint8_t *sfxdata = new uint8_t[size]; auto sfxdata = wlump.Read(size);
wlump.Read(sfxdata, size); int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]);
// If the sound is voc, use the custom loader. // 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. // If the sound is raw, just load it as such.
else if (sfx->bLoadRAW) 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. // 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; 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. // If that fails, let the sound system try and figure it out.
else else
{ {
snd = GSnd->LoadSound(sfxdata, size, true, pBuffer); snd = GSnd->LoadSound(sfxdata.Data(), size, true, pBuffer);
} }
delete[] sfxdata;
} }
sfx->data3d = snd.first; 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); 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) if (logarithmic)
{ {

View file

@ -165,8 +165,8 @@ public:
}; };
extern FRolloffInfo S_Rolloff; extern FRolloffInfo S_Rolloff;
extern uint8_t *S_SoundCurve; extern TArray<uint8_t> S_SoundCurve;
extern int S_SoundCurveSize;
// Information about one playing sound. // Information about one playing sound.
struct sector_t; struct sector_t;

View file

@ -249,14 +249,10 @@ bool FScanner::OpenFile (const char *name)
FileReader fr; FileReader fr;
if (!fr.OpenFile(name)) return false; if (!fr.OpenFile(name)) return false;
auto filesize = fr.GetLength(); auto filesize = fr.GetLength();
auto filebuf = new uint8_t[filesize]; auto filebuff = fr.Read();
if (fr.Read(filebuf, filesize) != filesize) if (filebuff.Size() == 0 && filesize > 0) return false;
{
delete[] filebuf; ScriptBuffer = FString((const char *)filebuff.Data(), filesize);
return false;
}
ScriptBuffer = FString((const char *)filebuf, filesize);
delete[] filebuf;
ScriptName = name; // This is used for error messages so the full file name is preferable ScriptName = name; // This is used for error messages so the full file name is preferable
LumpNum = -1; LumpNum = -1;
PrepareScript (); PrepareScript ();

View file

@ -688,8 +688,8 @@ static float GetRolloff(const FRolloffInfo *rolloff, float distance)
if(rolloff->RolloffType == ROLLOFF_Linear) if(rolloff->RolloffType == ROLLOFF_Linear)
return volume; return volume;
if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve != NULL) if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve.Size() > 0)
return S_SoundCurve[int(S_SoundCurveSize * (1.f - volume))] / 127.f; return S_SoundCurve[int(S_SoundCurve.Size() * (1.f - volume))] / 127.f;
return (powf(10.f, volume) - 1.f) / 9.f; return (powf(10.f, volume) - 1.f) / 9.f;
} }

View file

@ -357,7 +357,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
int i; int i;
FTextureID lump; FTextureID lump;
char buffer[12]; char buffer[12];
FTexture **charlumps; TArray<FTexture*> charLumps;
int maxyoffs; int maxyoffs;
bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false; bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false;
bool stcfn121 = false; bool stcfn121 = false;
@ -365,7 +365,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
noTranslate = notranslate; noTranslate = notranslate;
Lump = fdlump; Lump = fdlump;
Chars = new CharData[count]; Chars = new CharData[count];
charlumps = new FTexture *[count]; charLumps.Resize(count);
PatchRemap = new uint8_t[256]; PatchRemap = new uint8_t[256];
FirstChar = first; FirstChar = first;
LastChar = first + count - 1; 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++) for (i = 0; i < count; i++)
{ {
charlumps[i] = NULL; charLumps[i] = NULL;
mysnprintf (buffer, countof(buffer), nametemplate, i + start); mysnprintf (buffer, countof(buffer), nametemplate, i + start);
lump = TexMan.CheckForTexture(buffer, ETextureType::MiscPatch); 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()) !TexMan.CheckForTexture("STCFN122", ETextureType::MiscPatch).isValid())
{ {
// insert the incorrectly named '|' graphic in its correct position. // 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(); lump.SetInvalid();
stcfn121 = true; 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 // set the lump here only if it represents a valid texture
if (i != 124-start || !stcfn121) if (i != 124-start || !stcfn121)
charlumps[i] = pic; charLumps[i] = pic;
int height = pic->GetScaledHeight(); int height = pic->GetScaledHeight();
int yoffs = pic->GetScaledTopOffset(0); 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]); if (!noTranslate) Chars[i].Pic = new FFontChar1 (charLumps[i]);
else Chars[i].Pic = charlumps[i]; else Chars[i].Pic = charLumps[i];
Chars[i].XMove = Chars[i].Pic->GetScaledWidth(); Chars[i].XMove = Chars[i].Pic->GetScaledWidth();
} }
else else
@ -454,8 +454,6 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
FixXMoves(); FixXMoves();
if (!noTranslate) LoadTranslations(); if (!noTranslate) LoadTranslations();
delete[] charlumps;
} }
//========================================================================== //==========================================================================