Skip to content

Commit c59fafe

Browse files
committed
[GEN] Backports various memory fixes from ZH
1 parent c35b057 commit c59fafe

File tree

25 files changed

+249
-55
lines changed

25 files changed

+249
-55
lines changed

Generals/Code/GameEngine/Include/Common/Science.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class ScienceStore : public SubsystemInterface
8181
friend class ScienceInfo;
8282

8383
public:
84+
virtual ~ScienceStore();
85+
8486
void init();
8587
void reset();
8688
void update() { }

Generals/Code/GameEngine/Include/Common/SparseMatchFinder.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,22 @@ class SparseMatchFinder
176176
const MATCHABLE* findBestInfo(const std::vector<MATCHABLE>& v, const BITSET& bits) const
177177
{
178178
typename MatchMap::const_iterator it = m_bestMatches.find(bits);
179+
180+
const MATCHABLE *first = NULL;
179181
if (it != m_bestMatches.end())
180182
{
181-
return (*it).second;
183+
first = (*it).second;
182184
}
183-
185+
if (first != NULL) {
186+
return first;
187+
}
188+
184189
const MATCHABLE* info = findBestInfoSlow(v, bits);
185190

186191
DEBUG_ASSERTCRASH(info != NULL, ("no suitable match for criteria was found!\n"));
187-
if (info != NULL)
192+
if (info != NULL) {
188193
m_bestMatches[bits] = info;
194+
}
189195

190196
return info;
191197
}

Generals/Code/GameEngine/Include/GameLogic/RankInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ class RankInfo : public Overridable
4949
Int m_sciencePurchasePointsGranted;
5050
ScienceVec m_sciencesGranted;
5151
};
52-
EMPTY_DTOR(RankInfo)
52+
//EMPTY_DTOR(RankInfo)
5353

5454
//-------------------------------------------------------------------------------------------------
5555
class RankInfoStore : public SubsystemInterface
5656
{
57+
public:
58+
virtual ~RankInfoStore();
5759

5860
public:
5961
void init();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,10 @@ GlobalData::~GlobalData( void )
10551055
if (m_weaponBonusSet)
10561056
m_weaponBonusSet->deleteInstance();
10571057

1058-
if( m_theOriginal == this )
1058+
if( m_theOriginal == this ) {
10591059
m_theOriginal = NULL;
1060+
TheWritableGlobalData = NULL;
1061+
}
10601062

10611063
} // end ~GlobalData
10621064

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ LogClass::~LogClass()
6161

6262
void LogClass::log(const char *fmt, ...)
6363
{
64+
if (!m_fp)
65+
return;
6466
static char buf[1024];
6567
static Int lastFrame = 0;
6668
static Int lastIndex = 0;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ void ScienceStore::init()
4848
m_sciences.clear();
4949
}
5050

51+
//-----------------------------------------------------------------------------
52+
ScienceStore::~ScienceStore()
53+
{
54+
// nope.
55+
//m_sciences.clear();
56+
57+
// go through all sciences and delete any overrides
58+
for (ScienceInfoVec::iterator it = m_sciences.begin(); it != m_sciences.end(); /*++it*/)
59+
{
60+
ScienceInfo* si = *it;
61+
++it;
62+
if (si) {
63+
si->deleteInstance();
64+
}
65+
}
66+
}
67+
5168
//-----------------------------------------------------------------------------
5269
void ScienceStore::reset()
5370
{

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void doStackDump();
141141
inline Bool ignoringAsserts()
142142
{
143143
#ifdef DEBUG_CRASHING
144-
return !DX8Wrapper_IsWindowed || TheGlobalData->m_debugIgnoreAsserts;
144+
return !DX8Wrapper_IsWindowed || (TheGlobalData&&TheGlobalData->m_debugIgnoreAsserts);
145145
#else
146146
return !DX8Wrapper_IsWindowed;
147147
#endif
@@ -468,7 +468,7 @@ void DebugCrash(const char *format, ...)
468468
doLogOutput(theCrashBuffer);
469469
#endif
470470
#ifdef DEBUG_STACKTRACE
471-
if (!TheGlobalData->m_debugIgnoreStackTrace)
471+
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
472472
{
473473
doStackDump();
474474
}
@@ -663,6 +663,10 @@ void ReleaseCrash(const char *reason)
663663
char prevbuf[ _MAX_PATH ];
664664
char curbuf[ _MAX_PATH ];
665665

666+
if (TheGlobalData==NULL) {
667+
return; // We are shutting down, and TheGlobalData has been freed. jba. [4/15/2003]
668+
}
669+
666670
strcpy(prevbuf, TheGlobalData->getPath_UserData().str());
667671
strcat(prevbuf, RELEASECRASH_FILE_NAME_PREV);
668672
strcpy(curbuf, TheGlobalData->getPath_UserData().str());

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void Xfer::xferScienceType( ScienceType *science )
540540
else
541541
{
542542

543-
DEBUG_CRASH(( "xferScienceVec - Unknown xfer mode '%d'\n", getXferMode() ));
543+
DEBUG_CRASH(( "xferScienceType - Unknown xfer mode '%d'\n", getXferMode() ));
544544
throw XFER_MODE_UNKNOWN;
545545

546546
} // end else
@@ -577,8 +577,12 @@ void Xfer::xferScienceVec( ScienceVec *scienceVec )
577577
// vector should be empty at this point
578578
if( scienceVec->empty() == FALSE )
579579
{
580-
DEBUG_CRASH(( "xferScienceVec - vector is not empty, but should be\n" ));
581-
throw XFER_LIST_NOT_EMPTY;
580+
// Not worth an assert, since things can give you Sciences on creation. Just handle it and load.
581+
scienceVec->clear();
582+
583+
// Homework for today. Write 2000 words reconciling "Your code must never crash" with "Intentionally putting crashes in the code". Fucktard.
584+
// DEBUG_CRASH(( "xferScienceVec - vector is not empty, but should be\n" ));
585+
// throw XFER_LIST_NOT_EMPTY;
582586
}
583587

584588
for( UnsignedShort i = 0; i < count; ++i )

Generals/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ void ControlBar::populatePurchaseScience( Player* player )
252252

253253
setControlCommand( m_sciencePurchaseWindowsRank3[ i ], commandButton );
254254
ScienceType st = SCIENCE_INVALID;
255-
st = commandButton->getScienceVec()[ 0 ];
255+
ScienceVec sv = commandButton->getScienceVec();
256+
if (! sv.empty())
257+
{
258+
st = sv[ 0 ];
259+
}
256260

257261
if( player->isScienceDisabled( st ) )
258262
{
@@ -940,6 +944,10 @@ ControlBar::~ControlBar( void )
940944
}
941945

942946
m_radarAttackGlowWindow = NULL;
947+
948+
if (m_rightHUDCameoWindow && m_rightHUDCameoWindow->winGetUserData())
949+
delete m_rightHUDCameoWindow->winGetUserData();
950+
943951
} // end ~ControlBar
944952
void ControlBarPopupDescriptionUpdateFunc( WindowLayout *layout, void *param );
945953

@@ -1017,9 +1025,12 @@ void ControlBar::init( void )
10171025
id = TheNameKeyGenerator->nameToKey( windowName.str() );
10181026
m_commandWindows[ i ] =
10191027
TheWindowManager->winGetWindowFromId( m_contextParent[ CP_COMMAND ], id );
1020-
m_commandWindows[ i ]->winGetPosition(&commandPos.x, &commandPos.y);
1021-
m_commandWindows[ i ]->winGetSize(&commandSize.x, &commandSize.y);
1022-
m_commandWindows[ i ]->winSetStatus( WIN_STATUS_USE_OVERLAY_STATES );
1028+
if (m_commandWindows[ i ])
1029+
{
1030+
m_commandWindows[ i ]->winGetPosition(&commandPos.x, &commandPos.y);
1031+
m_commandWindows[ i ]->winGetSize(&commandSize.x, &commandSize.y);
1032+
m_commandWindows[ i ]->winSetStatus( WIN_STATUS_USE_OVERLAY_STATES );
1033+
}
10231034

10241035
// removed from multiplayer branch
10251036
// windowName.format( "ControlBar.wnd:CommandMarker%02d", i + 1 );
@@ -1052,7 +1063,7 @@ void ControlBar::init( void )
10521063
m_sciencePurchaseWindowsRank3[ i ]->winSetStatus( WIN_STATUS_USE_OVERLAY_STATES );
10531064
} // end for i
10541065

1055-
for( i = 0; i < MAX_PURCHASE_SCIENCE_RANK_8; i++ )
1066+
for( i = 0; i < MAX_PURCHASE_SCIENCE_RANK_8; i++ )
10561067
{
10571068
windowName.format( "GeneralsExpPoints.wnd:ButtonRank8Number%d", i );
10581069
id = TheNameKeyGenerator->nameToKey( windowName.str() );
@@ -1933,7 +1944,10 @@ CommandSet* ControlBar::findNonConstCommandSet( const AsciiString& name )
19331944
const CommandButton *ControlBar::findCommandButton( const AsciiString& name )
19341945
{
19351946
CommandButton *btn = findNonConstCommandButton(name);
1936-
btn = (CommandButton *)btn->friend_getFinalOverride();
1947+
if( btn )
1948+
{
1949+
btn = (CommandButton *)btn->friend_getFinalOverride();
1950+
}
19371951
return btn;
19381952
}
19391953

@@ -2059,7 +2073,11 @@ void ControlBar::switchToContext( ControlBarContext context, Drawable *draw )
20592073
//Clear any potentially flashing buttons!
20602074
for( int i = 0; i < MAX_COMMANDS_PER_SET; i++ )
20612075
{
2062-
m_commandWindows[ i ]->winClearStatus( WIN_STATUS_FLASHING );
2076+
// the implementation won't necessarily use the max number of windows possible
2077+
if (m_commandWindows[ i ])
2078+
{
2079+
m_commandWindows[ i ]->winClearStatus( WIN_STATUS_FLASHING );
2080+
}
20632081
}
20642082
// if there is a current selected drawable then we wil display a selection portrait if present
20652083
if( draw )

Generals/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ void ControlBar::doTransportInventoryUI( Object *transport, const CommandSet *co
179179
const CommandButton *commandButton;
180180
for( Int i = 0; i < MAX_COMMANDS_PER_SET; i++ )
181181
{
182+
// our implementation doesn't necessarily make use of the max possible command buttons
183+
if (! m_commandWindows[ i ]) continue;
182184

183185
// get command button
184186
commandButton = commandSet->getCommandButton(i);
@@ -213,6 +215,9 @@ void ControlBar::doTransportInventoryUI( Object *transport, const CommandSet *co
213215
m_commandWindows[ i ]->winHide( TRUE );
214216
}
215217

218+
219+
// is this where we set the cameos disabled when container is subdued?
220+
216221
// if we've counted more UI spots than the transport can hold, hide this command window
217222
if( inventoryCommandCount > transportMax )
218223
m_commandWindows[ i ]->winHide( TRUE );
@@ -279,7 +284,10 @@ void ControlBar::populateCommand( Object *obj )
279284

280285
// hide all the buttons
281286
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
282-
m_commandWindows[ i ]->winHide( TRUE );
287+
if (m_commandWindows[ i ])
288+
{
289+
m_commandWindows[ i ]->winHide( TRUE );
290+
}
283291

284292
// nothing left to do
285293
return;
@@ -294,6 +302,8 @@ void ControlBar::populateCommand( Object *obj )
294302
const CommandButton *commandButton;
295303
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
296304
{
305+
// our implementation doesn't necessarily make use of the max possible command buttons
306+
if (! m_commandWindows[ i ]) continue;
297307

298308
// get command button
299309
commandButton = commandSet->getCommandButton(i);
@@ -377,7 +387,7 @@ void ControlBar::populateCommand( Object *obj )
377387
//Now we have to search through the command buttons to find a matching purchase science button.
378388
for( const CommandButton *command = m_commandButtons; command; command = command->getNext() )
379389
{
380-
if( command->getCommandType() == GUI_COMMAND_PURCHASE_SCIENCE )
390+
if( command && command->getCommandType() == GUI_COMMAND_PURCHASE_SCIENCE )
381391
{
382392
//All purchase sciences specify a single science.
383393
if( command->getScienceVec().empty() )
@@ -732,6 +742,9 @@ void ControlBar::updateContextCommand( void )
732742
GameWindow *win;
733743
const CommandButton *command;
734744

745+
// our implementation doesn't necessarily make use of the max possible command buttons
746+
if (! m_commandWindows[ i ]) continue;
747+
735748
// get the window
736749
win = m_commandWindows[ i ];
737750

@@ -1277,7 +1290,7 @@ CommandAvailability ControlBar::getCommandAvailability( const CommandButton *com
12771290
}
12781291
else if( SpecialAbilityUpdate *spUpdate = obj->findSpecialAbilityUpdate( command->getSpecialPowerTemplate()->getSpecialPowerType() ) )
12791292
{
1280-
if( spUpdate->isPowerCurrentlyInUse( command ) )
1293+
if( spUpdate && spUpdate->isPowerCurrentlyInUse( command ) )
12811294
{
12821295
return COMMAND_RESTRICTED;
12831296
}

Generals/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,
9595
{
9696
// get the command pointer from the control user data we put in the button
9797
const CommandButton *commandButton = (const CommandButton *)GadgetButtonGetData(control);
98+
if( !commandButton )
99+
{
100+
DEBUG_CRASH( ("ControlBar::processCommandUI() -- Button activated has no data. Ignoring...") );
101+
return CBC_COMMAND_NOT_USED;
102+
}
98103

99104
// sanity, we won't process messages if we have no source object,
100105
// unless we're CB_CONTEXT_PURCHASE_SCIENCE or GUI_COMMAND_SPECIAL_POWER_FROM_COMMAND_CENTER

Generals/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarMultiSelect.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ void ControlBar::addCommonCommands( Drawable *draw, Bool firstDrawable )
9696
{
9797

9898
m_commonCommands[ i ] = NULL;
99-
m_commandWindows[ i ]->winHide( TRUE );
99+
if (m_commandWindows[ i ])
100+
{
101+
m_commandWindows[ i ]->winHide( TRUE );
102+
}
100103
// After Every change to the m_commandWIndows, we need to show fill in the missing blanks with the images
101104
// removed from multiplayer branch
102105
//showCommandMarkers();
@@ -118,6 +121,8 @@ void ControlBar::addCommonCommands( Drawable *draw, Bool firstDrawable )
118121
// just add each command that is classified as a common command
119122
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
120123
{
124+
// our implementation doesn't necessarily make use of the max possible command buttons
125+
if (! m_commandWindows[ i ]) continue;
121126

122127
// get command
123128
command = commandSet->getCommandButton(i);
@@ -148,6 +153,9 @@ void ControlBar::addCommonCommands( Drawable *draw, Bool firstDrawable )
148153
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
149154
{
150155

156+
// our implementation doesn't necessarily make use of the max possible command buttons
157+
if (! m_commandWindows[ i ]) continue;
158+
151159
// get the command
152160
command = commandSet->getCommandButton(i);
153161

@@ -213,7 +221,12 @@ void ControlBar::populateMultiSelect( void )
213221

214222
// by default, hide all the controls in the command section
215223
for( Int i = 0; i < MAX_COMMANDS_PER_SET; i++ )
216-
m_commandWindows[ i ]->winHide( TRUE );
224+
{
225+
if (m_commandWindows[ i ])
226+
{
227+
m_commandWindows[ i ]->winHide( TRUE );
228+
}
229+
}
217230

218231
// sanity
219232
DEBUG_ASSERTCRASH( TheInGameUI->getSelectCount() > 1,
@@ -333,6 +346,9 @@ void ControlBar::updateContextMultiSelect( void )
333346
// get the control window
334347
win = m_commandWindows[ i ];
335348

349+
// our implementation doesn't necessarily make use of the max possible command buttons
350+
if (!win) continue;
351+
336352
// don't consider hidden windows
337353
if( win->winIsHidden() == TRUE )
338354
continue;
@@ -389,6 +405,8 @@ void ControlBar::updateContextMultiSelect( void )
389405
//
390406
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
391407
{
408+
// our implementation doesn't necessarily make use of the max possible command buttons
409+
if (! m_commandWindows[ i ]) continue;
392410

393411
// don't consider hidden commands
394412
if( m_commandWindows[ i ]->winIsHidden() == TRUE )

0 commit comments

Comments
 (0)