Skip to content

Commit db0730f

Browse files
authored
[GEN][ZH] Clear and assert GameWindow user data to avoid dangling pointers and memory leaks (#1027)
1 parent d981516 commit db0730f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+166
-50
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,10 @@ ControlBar::~ControlBar( void )
10161016
m_radarAttackGlowWindow = NULL;
10171017

10181018
if (m_rightHUDCameoWindow && m_rightHUDCameoWindow->winGetUserData())
1019+
{
10191020
delete m_rightHUDCameoWindow->winGetUserData();
1021+
m_rightHUDCameoWindow->winSetUserData(NULL);
1022+
}
10201023

10211024
} // end ~ControlBar
10221025
void ControlBarPopupDescriptionUpdateFunc( WindowLayout *layout, void *param );

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,11 @@ void DeinitLanGameGadgets( void )
751751
listboxChatWindowLanGame = NULL;
752752
textEntryChat = NULL;
753753
textEntryMapDisplay = NULL;
754-
windowMap = NULL;
754+
if (windowMap)
755+
{
756+
windowMap->winSetUserData(NULL);
757+
windowMap = NULL;
758+
}
755759
for (Int i = 0; i < MAX_SLOTS; i++)
756760
{
757761
comboBoxPlayer[i] = NULL;
@@ -1035,6 +1039,9 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m
10351039
//-------------------------------------------------------------------------------------------------
10361040
case GWM_DESTROY:
10371041
{
1042+
if (windowMap)
1043+
windowMap->winSetUserData(NULL);
1044+
10381045
break;
10391046
} // case GWM_DESTROY:
10401047
//-------------------------------------------------------------------------------------------------

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ static void showLANGameOptionsUnderlyingGUIElements( Bool show )
106106

107107
static void NullifyControls()
108108
{
109-
mapList = NULL;
110-
winMapPreview = NULL;
111109
parent = NULL;
110+
mapList = NULL;
111+
if (winMapPreview)
112+
{
113+
winMapPreview->winSetUserData(NULL);
114+
winMapPreview = NULL;
115+
}
112116
for (Int i=0; i<MAX_SLOTS; ++i)
113117
{
114118
buttonMapStartPosition[i] = NULL;

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@ WindowMsgHandledType SkirmishGameOptionsMenuSystem( GameWindow *window, Unsigned
14531453
//-------------------------------------------------------------------------------------------------
14541454
case GWM_DESTROY:
14551455
{
1456+
if (windowMap)
1457+
windowMap->winSetUserData(NULL);
1458+
14561459
break;
14571460
} // case GWM_DESTROY:
14581461
//-------------------------------------------------------------------------------------------------

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ static NameKeyType winMapPreviewID = NAMEKEY_INVALID;
6767

6868
static void NullifyControls()
6969
{
70-
mapList = NULL;
71-
winMapPreview = NULL;
7270
parent = NULL;
71+
mapList = NULL;
72+
if (winMapPreview)
73+
{
74+
winMapPreview->winSetUserData(NULL);
75+
winMapPreview = NULL;
76+
}
7377
for (Int i=0; i<MAX_SLOTS; ++i)
7478
{
7579
buttonMapStartPosition[i] = NULL;

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,11 @@ void DeinitWOLGameGadgets( void )
11471147
listboxGameSetupChat = NULL;
11481148
textEntryChat = NULL;
11491149
textEntryMapDisplay = NULL;
1150-
windowMap = NULL;
1150+
if (windowMap)
1151+
{
1152+
windowMap->winSetUserData(NULL);
1153+
windowMap = NULL;
1154+
}
11511155
// GameWindow *staticTextTitle = NULL;
11521156
for (Int i = 0; i < MAX_SLOTS; i++)
11531157
{
@@ -2407,6 +2411,9 @@ WindowMsgHandledType WOLGameSetupMenuSystem( GameWindow *window, UnsignedInt msg
24072411
//-------------------------------------------------------------------------------------------------
24082412
case GWM_DESTROY:
24092413
{
2414+
if (windowMap)
2415+
windowMap->winSetUserData(NULL);
2416+
24102417
break;
24112418
} // case GWM_DESTROY:
24122419
//-------------------------------------------------------------------------------------------------

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ static GameWindow *winMapWindow = NULL;
7777
static void NullifyControls(void)
7878
{
7979
parent = NULL;
80-
winMapPreview = NULL;
8180
mapList = NULL;
81+
if (winMapPreview)
82+
{
83+
winMapPreview->winSetUserData(NULL);
84+
winMapPreview = NULL;
85+
}
8286
for (Int i=0; i<MAX_SLOTS; ++i)
8387
{
8488
buttonMapStartPosition[i] = NULL;

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetComboBox.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,9 @@ WindowMsgHandledType GadgetComboBoxSystem( GameWindow *window, UnsignedInt msg,
595595
case GWM_DESTROY:
596596
{
597597
TheWindowManager->winSetLoneWindow(NULL); // if we are transitioning screens, close all combo boxes
598-
if (comboData)
599-
{
600-
delete(comboData);
601-
comboData = NULL;
602-
}
598+
delete (ComboBoxData *)window->winGetUserData();
599+
window->winSetUserData(NULL);
600+
comboData = NULL;
603601
break;
604602
} // end destroy
605603

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetHorizontalSlider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ WindowMsgHandledType GadgetHorizontalSliderSystem( GameWindow *window, UnsignedI
437437
// ------------------------------------------------------------------------
438438
case GWM_DESTROY:
439439
delete ( (SliderData *)window->winGetUserData() );
440+
window->winSetUserData(NULL);
441+
s = NULL;
440442
break;
441443

442444
// ------------------------------------------------------------------------

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetListBox.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,11 @@ WindowMsgHandledType GadgetListBoxSystem( GameWindow *window, UnsignedInt msg,
19801980
delete[]( list->columnWidthPercentage );
19811981
if( list->multiSelect )
19821982
delete[]( list->selections );
1983-
delete( list );
1983+
1984+
delete (ListboxData *)window->winGetUserData();
1985+
window->winSetUserData( NULL );
1986+
list = NULL;
1987+
19841988
break;
19851989

19861990
} // end destroy

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetProgressBar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ WindowMsgHandledType GadgetProgressBarSystem( GameWindow *window, UnsignedInt ms
7575

7676
switch( msg )
7777
{
78+
// ------------------------------------------------------------------------
79+
case GWM_DESTROY:
80+
{
81+
window->winSetUserData( NULL );
82+
break;
83+
}
7884

7985
// ------------------------------------------------------------------------
8086
case GPM_SET_PROGRESS:

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetPushButton.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,10 @@ WindowMsgHandledType GadgetPushButtonSystem( GameWindow *window, UnsignedInt msg
442442
// ------------------------------------------------------------------------
443443
case GWM_DESTROY:
444444
{
445-
PushButtonData *pData = (PushButtonData *)window->winGetUserData();
446-
if(pData)
447-
delete pData;
445+
delete (PushButtonData *)window->winGetUserData();
448446
window->winSetUserData(NULL);
449-
}
450447
break;
448+
}
451449

452450
// ------------------------------------------------------------------------
453451
case GWM_INPUT_FOCUS:

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetRadioButton.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ WindowMsgHandledType GadgetRadioButtonSystem( GameWindow *window, UnsignedInt ms
370370
// ------------------------------------------------------------------------
371371
case GWM_DESTROY:
372372
{
373-
RadioButtonData *radioData = (RadioButtonData *)window->winGetUserData();
374-
375373
// free radio button user data
376-
delete radioData;
374+
delete (RadioButtonData *)window->winGetUserData();
375+
window->winSetUserData( NULL );
377376

378377
break;
379378

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetStaticText.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ WindowMsgHandledType GadgetStaticTextSystem( GameWindow *window, UnsignedInt msg
159159
TheDisplayStringManager->freeDisplayString( data->text );
160160

161161
// free text data
162-
delete( data );
162+
delete (TextData *)window->winGetUserData();
163+
window->winSetUserData( NULL );
163164

164165
break;
165166

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetTabControl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ WindowMsgHandledType GadgetTabControlSystem( GameWindow *tabControl, UnsignedInt
141141
// ------------------------------------------------------------------------
142142
case GWM_DESTROY:
143143
{
144-
TabControlData *tabData = (TabControlData *)tabControl->winGetUserData();
145-
146144
// free tab control user data
147-
delete tabData;
145+
delete (TabControlData *)tabControl->winGetUserData();
146+
tabControl->winSetUserData( NULL );
148147

149148
break;
150149

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetTextEntry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ WindowMsgHandledType GadgetTextEntrySystem( GameWindow *window, UnsignedInt msg,
370370

371371
// free all edit data
372372
delete( (EntryData *)window->winGetUserData() );
373+
window->winSetUserData(NULL);
374+
e = NULL;
373375
break;
374376

375377
// ------------------------------------------------------------------------

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetVerticalSlider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ WindowMsgHandledType GadgetVerticalSliderSystem( GameWindow *window, UnsignedInt
426426
// ------------------------------------------------------------------------
427427
case GWM_DESTROY:
428428
delete( (SliderData *)window->winGetUserData() );
429+
window->winSetUserData(NULL);
430+
s = NULL;
429431
break;
430432

431433
// ------------------------------------------------------------------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ GameWindow::GameWindow( void )
9999
m_cursorX = 0;
100100
m_cursorY = 0;
101101

102-
m_userData = 0;
102+
m_userData = NULL;
103103

104104
m_inputData = NULL;
105105

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ void GameWindowManager::processDestroyList( void )
120120
// send the destroy message to the window we're about to kill
121121
winSendSystemMsg( doDestroy, GWM_DESTROY, 0, 0 );
122122

123+
DEBUG_ASSERTCRASH(doDestroy->winGetUserData() == NULL, ("Win user data is expected to be deleted now"));
124+
123125
// free the memory
124126
if (doDestroy)
125127
deleteInstance(doDestroy);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ IMEManager::~IMEManager()
534534
{
535535
if ( m_candidateWindow )
536536
{
537+
m_candidateWindow->winSetUserData( NULL );
538+
m_candidateTextArea->winSetUserData( NULL );
539+
537540
TheWindowManager->winDestroy( m_candidateWindow );
538541
}
539542

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ MultiPlayerLoadScreen::MultiPlayerLoadScreen( void )
759759

760760
MultiPlayerLoadScreen::~MultiPlayerLoadScreen( void )
761761
{
762+
if(m_mapPreview)
763+
{
764+
m_mapPreview->winSetUserData(NULL);
765+
}
766+
762767
for(Int i = 0; i < MAX_SLOTS; ++i)
763768
{
764769
m_progressBars[i] = NULL;
@@ -984,6 +989,11 @@ GameSpyLoadScreen::GameSpyLoadScreen( void )
984989

985990
GameSpyLoadScreen::~GameSpyLoadScreen( void )
986991
{
992+
if(m_mapPreview)
993+
{
994+
m_mapPreview->winSetUserData(NULL);
995+
}
996+
987997
for(Int i = 0; i < MAX_SLOTS; ++i)
988998
{
989999
m_progressBars[i] = NULL;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,10 @@ ControlBar::~ControlBar( void )
10391039
m_radarAttackGlowWindow = NULL;
10401040

10411041
if (m_rightHUDCameoWindow && m_rightHUDCameoWindow->winGetUserData())
1042+
{
10421043
delete m_rightHUDCameoWindow->winGetUserData();
1044+
m_rightHUDCameoWindow->winSetUserData(NULL);
1045+
}
10431046

10441047
} // end ~ControlBar
10451048
void ControlBarPopupDescriptionUpdateFunc( WindowLayout *layout, void *param );

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,11 @@ void DeinitLanGameGadgets( void )
827827
textEntryMapDisplay = NULL;
828828
checkboxLimitSuperweapons = NULL;
829829
comboBoxStartingCash = NULL;
830-
windowMap = NULL;
830+
if (windowMap)
831+
{
832+
windowMap->winSetUserData(NULL);
833+
windowMap = NULL;
834+
}
831835
for (Int i = 0; i < MAX_SLOTS; i++)
832836
{
833837
comboBoxPlayer[i] = NULL;
@@ -1130,6 +1134,9 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m
11301134
//-------------------------------------------------------------------------------------------------
11311135
case GWM_DESTROY:
11321136
{
1137+
if (windowMap)
1138+
windowMap->winSetUserData(NULL);
1139+
11331140
break;
11341141
} // case GWM_DESTROY:
11351142
//-------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanMapSelectMenu.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ static void showLANGameOptionsUnderlyingGUIElements( Bool show )
106106

107107
static void NullifyControls()
108108
{
109-
mapList = NULL;
110-
winMapPreview = NULL;
111109
parent = NULL;
110+
mapList = NULL;
111+
if (winMapPreview)
112+
{
113+
winMapPreview->winSetUserData(NULL);
114+
winMapPreview = NULL;
115+
}
112116
for (Int i=0; i<MAX_SLOTS; ++i)
113117
{
114118
buttonMapStartPosition[i] = NULL;

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,9 @@ WindowMsgHandledType SkirmishGameOptionsMenuSystem( GameWindow *window, Unsigned
15851585
//-------------------------------------------------------------------------------------------------
15861586
case GWM_DESTROY:
15871587
{
1588+
if (windowMap)
1589+
windowMap->winSetUserData(NULL);
1590+
15881591
break;
15891592
} // case GWM_DESTROY:
15901593
//-------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishMapSelectMenu.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ static NameKeyType winMapPreviewID = NAMEKEY_INVALID;
6767

6868
static void NullifyControls()
6969
{
70-
mapList = NULL;
71-
winMapPreview = NULL;
7270
parent = NULL;
71+
mapList = NULL;
72+
if (winMapPreview)
73+
{
74+
winMapPreview->winSetUserData(NULL);
75+
winMapPreview = NULL;
76+
}
7377
for (Int i=0; i<MAX_SLOTS; ++i)
7478
{
7579
buttonMapStartPosition[i] = NULL;

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,11 @@ void DeinitWOLGameGadgets( void )
13001300
listboxGameSetupChat = NULL;
13011301
textEntryChat = NULL;
13021302
textEntryMapDisplay = NULL;
1303-
windowMap = NULL;
1303+
if (windowMap)
1304+
{
1305+
windowMap->winSetUserData(NULL);
1306+
windowMap = NULL;
1307+
}
13041308
checkBoxUseStats = NULL;
13051309
checkBoxLimitSuperweapons = NULL;
13061310
comboBoxStartingCash = NULL;
@@ -2595,6 +2599,9 @@ WindowMsgHandledType WOLGameSetupMenuSystem( GameWindow *window, UnsignedInt msg
25952599
//-------------------------------------------------------------------------------------------------
25962600
case GWM_DESTROY:
25972601
{
2602+
if (windowMap)
2603+
windowMap->winSetUserData(NULL);
2604+
25982605
break;
25992606
} // case GWM_DESTROY:
26002607
//-------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ static GameWindow *winMapWindow = NULL;
7777
static void NullifyControls(void)
7878
{
7979
parent = NULL;
80-
winMapPreview = NULL;
8180
mapList = NULL;
81+
if (winMapPreview)
82+
{
83+
winMapPreview->winSetUserData(NULL);
84+
winMapPreview = NULL;
85+
}
8286
for (Int i=0; i<MAX_SLOTS; ++i)
8387
{
8488
buttonMapStartPosition[i] = NULL;

0 commit comments

Comments
 (0)