Skip to content

Commit 20f4254

Browse files
authored
fix(memory): Fix various memory leaks (2) (TheSuperHackers#2710)
1 parent ed7e96f commit 20f4254

19 files changed

Lines changed: 235 additions & 132 deletions

File tree

Core/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ void GameLogic::clearGameData( Bool showScoreScreen )
272272

273273
void FixupScoreScreenMovieWindow();
274274
FixupScoreScreenMovieWindow();
275+
276+
destroyQuitMenu();
275277
}
276278

277279
TheGameEngine->reset();

Core/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,51 +2180,53 @@ void W3DModelDraw::adjustAnimation(const ModelConditionInfo* prevState, Real pre
21802180

21812181
const W3DAnimationInfo& animInfo = m_curState->m_animations[m_whichAnimInCurState];
21822182

2183-
HAnimClass* animHandle = animInfo.getAnimHandle(); // note that this now returns an ADDREFED handle, which must be released by the caller!
2184-
if (m_renderObject && animHandle)
2183+
if (m_renderObject)
21852184
{
2186-
Int startFrame = 0;
2187-
if (m_curState->m_mode == RenderObjClass::ANIM_MODE_ONCE_BACKWARDS ||
2188-
m_curState->m_mode == RenderObjClass::ANIM_MODE_LOOP_BACKWARDS)
2185+
HAnimClass* animHandle = animInfo.getAnimHandle(); // note that this now returns an ADDREFED handle, which must be released by the caller!
2186+
if (animHandle)
21892187
{
2190-
startFrame = animHandle->Get_Num_Frames()-1;
2191-
}
2188+
Int startFrame = 0;
2189+
if (m_curState->m_mode == RenderObjClass::ANIM_MODE_ONCE_BACKWARDS ||
2190+
m_curState->m_mode == RenderObjClass::ANIM_MODE_LOOP_BACKWARDS)
2191+
{
2192+
startFrame = animHandle->Get_Num_Frames()-1;
2193+
}
21922194

2193-
if (testFlagBit(m_curState->m_flags, RANDOMIZE_START_FRAME))
2194-
{
2195-
startFrame = GameClientRandomValue(0, animHandle->Get_Num_Frames()-1);
2196-
}
2197-
else if (testFlagBit(m_curState->m_flags, START_FRAME_FIRST))
2198-
{
2199-
startFrame = 0;
2200-
}
2201-
else if (testFlagBit(m_curState->m_flags, START_FRAME_LAST))
2202-
{
2203-
startFrame = animHandle->Get_Num_Frames()-1;
2204-
}
2205-
// order is important here: MAINTAIN_FRAME_ACROSS_STATES is overridden by the other bits, above.
2206-
else if (isAnyMaintainFrameFlagSet(m_curState->m_flags) &&
2207-
prevState &&
2208-
prevState != m_curState &&
2209-
isAnyMaintainFrameFlagSet(prevState->m_flags) &&
2210-
isCommonMaintainFrameFlagSet(m_curState->m_flags, prevState->m_flags) &&
2211-
prevAnimFraction >= 0.0)
2212-
{
2213-
startFrame = REAL_TO_INT(prevAnimFraction * animHandle->Get_Num_Frames()-1);
2214-
}
2195+
if (testFlagBit(m_curState->m_flags, RANDOMIZE_START_FRAME))
2196+
{
2197+
startFrame = GameClientRandomValue(0, animHandle->Get_Num_Frames()-1);
2198+
}
2199+
else if (testFlagBit(m_curState->m_flags, START_FRAME_FIRST))
2200+
{
2201+
startFrame = 0;
2202+
}
2203+
else if (testFlagBit(m_curState->m_flags, START_FRAME_LAST))
2204+
{
2205+
startFrame = animHandle->Get_Num_Frames()-1;
2206+
}
2207+
// order is important here: MAINTAIN_FRAME_ACROSS_STATES is overridden by the other bits, above.
2208+
else if (isAnyMaintainFrameFlagSet(m_curState->m_flags) &&
2209+
prevState &&
2210+
prevState != m_curState &&
2211+
isAnyMaintainFrameFlagSet(prevState->m_flags) &&
2212+
isCommonMaintainFrameFlagSet(m_curState->m_flags, prevState->m_flags) &&
2213+
prevAnimFraction >= 0.0)
2214+
{
2215+
startFrame = REAL_TO_INT(prevAnimFraction * animHandle->Get_Num_Frames()-1);
2216+
}
22152217

2216-
m_renderObject->Set_Animation(animHandle, startFrame, m_curState->m_mode);
2217-
REF_PTR_RELEASE(animHandle);
2218-
animHandle = nullptr;
2218+
m_renderObject->Set_Animation(animHandle, startFrame, m_curState->m_mode);
2219+
REF_PTR_RELEASE(animHandle);
2220+
animHandle = nullptr;
22192221

2220-
if (m_renderObject->Class_ID() == RenderObjClass::CLASSID_HLOD)
2221-
{
2222-
HLodClass *hlod = (HLodClass*)m_renderObject;
2223-
Real factor = GameClientRandomValueReal( m_curState->m_animMinSpeedFactor, m_curState->m_animMaxSpeedFactor );
2224-
hlod->Set_Animation_Frame_Rate_Multiplier( factor );
2222+
if (m_renderObject->Class_ID() == RenderObjClass::CLASSID_HLOD)
2223+
{
2224+
HLodClass *hlod = (HLodClass*)m_renderObject;
2225+
Real factor = GameClientRandomValueReal( m_curState->m_animMinSpeedFactor, m_curState->m_animMaxSpeedFactor );
2226+
hlod->Set_Animation_Frame_Rate_Multiplier( factor );
2227+
}
22252228
}
22262229
}
2227-
22282230
}
22292231
else
22302232
{

Core/Libraries/Source/WWVegas/WW3D2/hcanim.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,6 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
293293
/*
294294
** Now, read in all of the other chunks (motion channels).
295295
*/
296-
TimeCodedMotionChannelClass * tc_chan;
297-
AdaptiveDeltaMotionChannelClass * ad_chan;
298-
TimeCodedBitChannelClass * newbitchan;
299-
300296
while (cload.Open_Chunk()) {
301297

302298
switch (cload.Cur_Chunk_ID()) {
@@ -306,7 +302,8 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
306302
switch ( Flavor ) {
307303

308304
case ANIM_FLAVOR_TIMECODED:
309-
305+
{
306+
TimeCodedMotionChannelClass* tc_chan = nullptr;
310307
if (!read_channel(cload,&tc_chan)) {
311308
goto Error;
312309
}
@@ -322,8 +319,11 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
322319
}
323320

324321
break;
322+
}
325323

326324
case ANIM_FLAVOR_ADAPTIVE_DELTA:
325+
{
326+
AdaptiveDeltaMotionChannelClass* ad_chan = nullptr;
327327
if (!read_channel(cload,&ad_chan)) {
328328
goto Error;
329329
}
@@ -338,10 +338,13 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
338338
WWDEBUG_SAY(("ERROR! animation %s indexes a bone not present in the model. Please re-export!",Name));
339339
}
340340
break;
341+
}
341342
}
342343
break;
343344

344345
case W3D_CHUNK_COMPRESSED_BIT_CHANNEL:
346+
{
347+
TimeCodedBitChannelClass* newbitchan = nullptr;
345348
if (!read_bit_channel(cload,&newbitchan)) {
346349
goto Error;
347350
}
@@ -357,6 +360,7 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
357360
}
358361

359362
break;
363+
}
360364

361365
default:
362366
break;
@@ -387,20 +391,32 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
387391
*=============================================================================================*/
388392
bool HCompressedAnimClass::read_channel(ChunkLoadClass & cload,TimeCodedMotionChannelClass * * newchan)
389393
{
390-
*newchan = W3DNEW TimeCodedMotionChannelClass;
391-
bool result = (*newchan)->Load_W3D(cload);
392-
393-
return result;
394-
394+
TimeCodedMotionChannelClass* channel = W3DNEW TimeCodedMotionChannelClass;
395+
if (channel->Load_W3D(cload))
396+
{
397+
*newchan = channel;
398+
return true;
399+
}
400+
else
401+
{
402+
delete channel;
403+
return false;
404+
}
395405
}
396406

397407
bool HCompressedAnimClass::read_channel(ChunkLoadClass & cload,AdaptiveDeltaMotionChannelClass * * newchan)
398408
{
399-
*newchan = W3DNEW AdaptiveDeltaMotionChannelClass;
400-
bool result = (*newchan)->Load_W3D(cload);
401-
402-
return result;
403-
409+
AdaptiveDeltaMotionChannelClass* channel = W3DNEW AdaptiveDeltaMotionChannelClass;
410+
if (channel->Load_W3D(cload))
411+
{
412+
*newchan = channel;
413+
return true;
414+
}
415+
else
416+
{
417+
delete channel;
418+
return false;
419+
}
404420
}
405421

406422

@@ -483,11 +499,17 @@ void HCompressedAnimClass::add_channel(AdaptiveDeltaMotionChannelClass * newchan
483499
*=============================================================================================*/
484500
bool HCompressedAnimClass::read_bit_channel(ChunkLoadClass & cload,TimeCodedBitChannelClass * * newchan)
485501
{
486-
*newchan = W3DNEW TimeCodedBitChannelClass;
487-
bool result = (*newchan)->Load_W3D(cload);
488-
489-
return result;
490-
502+
TimeCodedBitChannelClass* channel = W3DNEW TimeCodedBitChannelClass;
503+
if (channel->Load_W3D(cload))
504+
{
505+
*newchan = channel;
506+
return true;
507+
}
508+
else
509+
{
510+
delete channel;
511+
return false;
512+
}
491513
}
492514

493515

Core/Libraries/Source/WWVegas/WW3D2/seglinerenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void SegLineRendererClass::Set_Texture_Tile_Factor(float factor)
191191
///@todo: I raised this number and didn't see much difference on our min-spec. -MW
192192
const static float MAX_LINE_TILING_FACTOR = 50.0f;
193193
if (factor > MAX_LINE_TILING_FACTOR) {
194-
WWDEBUG_SAY(("Texture (%s) Tile Factor (%.2f) too large in SegLineRendererClass!", Get_Texture()->Get_Texture_Name().str(), TextureTileFactor));
194+
WWDEBUG_SAY(("Texture (%s) Tile Factor (%.2f) too large in SegLineRendererClass!", Peek_Texture()->Get_Texture_Name().str(), TextureTileFactor));
195195
factor = MAX_LINE_TILING_FACTOR;
196196
} else {
197197
factor = MAX(factor, 0.0f);

Generals/Code/GameEngine/Source/Common/GameEngine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ GameEngine::~GameEngine()
278278
delete TheSubsystemList;
279279
TheSubsystemList = nullptr;
280280

281+
delete TheSkirmishGameInfo;
282+
TheSkirmishGameInfo = nullptr;
283+
281284
delete TheNetwork;
282285
TheNetwork = nullptr;
283286

Generals/Code/GameEngine/Source/Common/RTS/Player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,8 +2978,8 @@ void Player::removeUpgrade( const UpgradeTemplate *upgradeTemplate )
29782978
if( upgrade->getStatus() == UPGRADE_STATUS_COMPLETE )
29792979
onUpgradeRemoved();
29802980

2981+
deleteInstance(upgrade);
29812982
}
2982-
29832983
}
29842984

29852985

Generals/Code/GameEngine/Source/Common/System/SaveGame/GameStateMap.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static void embedPristineMap( AsciiString map, Xfer *xfer )
102102
if( file->read( buffer, fileSize ) != fileSize )
103103
{
104104

105+
delete[] buffer;
106+
105107
DEBUG_CRASH(( "embedPristineMap - Error reading from file '%s'", map.str() ));
106108
throw SC_INVALID_DATA;
107109

@@ -159,6 +161,8 @@ static void embedInUseMap( AsciiString map, Xfer *xfer )
159161
if( fread( buffer, 1, fileSize, fp ) != fileSize )
160162
{
161163

164+
delete[] buffer;
165+
162166
DEBUG_CRASH(( "embedInUseMap - Error reading from file '%s'", map.str() ));
163167
throw SC_INVALID_DATA;
164168

@@ -214,6 +218,8 @@ static void extractAndSaveMap( AsciiString mapToSave, Xfer *xfer )
214218
if( fwrite( buffer, 1, dataSize, fp ) != dataSize )
215219
{
216220

221+
delete[] buffer;
222+
217223
DEBUG_CRASH(( "extractAndSaveMap - Error writing to file '%s'", mapToSave.str() ));
218224
throw SC_INVALID_DATA;
219225

Generals/Code/GameEngine/Source/GameLogic/Map/SidesList.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,12 @@ void SidesList::prepareForMP_or_Skirmish()
534534
break;
535535
}
536536
}
537-
if (curSide == -1) continue;
537+
if (curSide == -1)
538+
{
539+
deleteInstance(scripts[i]);
540+
scripts[i] = nullptr;
541+
continue;
542+
}
538543

539544
deleteInstance(getSkirmishSideInfo(curSide)->getScriptList());
540545
getSkirmishSideInfo(curSide)->setScriptList(scripts[i]);

Generals/Code/GameEngine/Source/GameLogic/Object/Update/BattlePlanUpdate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ BattlePlanUpdate::~BattlePlanUpdate()
164164
TheAudio->removeAudioEvent( m_holdTheLineUnpack.getPlayingHandle() );
165165
TheAudio->removeAudioEvent( m_holdTheLinePack.getPlayingHandle() );
166166

167+
deleteInstance(m_bonuses);
167168
}
168169

169170
// ------------------------------------------------------------------------------------------------

Generals/Code/Libraries/Source/WWVegas/WW3D2/hrawanim.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,13 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
236236
/*
237237
** Now, read in all of the other chunks (motion channels).
238238
*/
239-
MotionChannelClass * newchan;
240-
BitChannelClass * newbitchan;
241-
242239
while (cload.Open_Chunk()) {
243240

244241
switch (cload.Cur_Chunk_ID()) {
245242

246243
case W3D_CHUNK_ANIMATION_CHANNEL:
244+
{
245+
MotionChannelClass* newchan = nullptr;
247246
if (!read_channel(cload,&newchan,pre30)) {
248247
goto Error;
249248
}
@@ -258,8 +257,11 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
258257
delete newchan;
259258
}
260259
break;
260+
}
261261

262262
case W3D_CHUNK_BIT_CHANNEL:
263+
{
264+
BitChannelClass* newbitchan = nullptr;
263265
if (!read_bit_channel(cload,&newbitchan,pre30)) {
264266
goto Error;
265267
}
@@ -274,6 +276,7 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
274276
delete newbitchan;
275277
}
276278
break;
279+
}
277280

278281
default:
279282
break;
@@ -304,14 +307,22 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
304307
*=============================================================================================*/
305308
bool HRawAnimClass::read_channel(ChunkLoadClass & cload,MotionChannelClass * * newchan,bool pre30)
306309
{
307-
*newchan = W3DNEW MotionChannelClass;
308-
bool result = (*newchan)->Load_W3D(cload);
310+
MotionChannelClass* channel = W3DNEW MotionChannelClass;
311+
if (channel->Load_W3D(cload))
312+
{
313+
if (pre30)
314+
{
315+
channel->PivotIdx += 1;
316+
}
309317

310-
if (result && pre30) {
311-
(*newchan)->PivotIdx += 1;
318+
*newchan = channel;
319+
return true;
320+
}
321+
else
322+
{
323+
delete channel;
324+
return false;
312325
}
313-
314-
return result;
315326
}
316327

317328
/***********************************************************************************************
@@ -378,14 +389,22 @@ void HRawAnimClass::add_channel(MotionChannelClass * newchan)
378389
*=============================================================================================*/
379390
bool HRawAnimClass::read_bit_channel(ChunkLoadClass & cload,BitChannelClass * * newchan,bool pre30)
380391
{
381-
*newchan = W3DNEW BitChannelClass;
382-
bool result = (*newchan)->Load_W3D(cload);
392+
BitChannelClass* channel = W3DNEW BitChannelClass;
393+
if (channel->Load_W3D(cload))
394+
{
395+
if (pre30)
396+
{
397+
channel->PivotIdx += 1;
398+
}
383399

384-
if (result && pre30) {
385-
(*newchan)->PivotIdx += 1;
400+
*newchan = channel;
401+
return true;
402+
}
403+
else
404+
{
405+
delete channel;
406+
return false;
386407
}
387-
388-
return result;
389408
}
390409

391410

0 commit comments

Comments
 (0)