Skip to content

Commit 9c8b539

Browse files
authored
[GEN][ZH] Fix Memory Manager initialization issues (#1275)
1 parent e5110b6 commit 9c8b539

File tree

6 files changed

+76
-94
lines changed

6 files changed

+76
-94
lines changed

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

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ inline HWND getThreadHWND()
166166
int MessageBoxWrapper( LPCSTR lpText, LPCSTR lpCaption, UINT uType )
167167
{
168168
HWND threadHWND = getThreadHWND();
169-
if (!threadHWND)
170-
return (uType & MB_ABORTRETRYIGNORE)?IDIGNORE:IDYES;
171-
172169
return ::MessageBox(threadHWND, lpText, lpCaption, uType);
173170
}
174171

@@ -365,8 +362,9 @@ void DebugInit(int flags)
365362
#ifdef DEBUG_LOGGING
366363

367364
// TheSuperHackers @info Debug initialization can happen very early.
368-
// Therefore, parse initial commandline and initialize the client instance now.
365+
// Determine the client instance id before creating the log file with an instance specific name.
369366
CommandLine::parseCommandLineForStartup();
367+
370368
if (!rts::ClientInstance::initialize())
371369
return;
372370

@@ -482,9 +480,11 @@ const char* DebugGetLogFileNamePrev()
482480
// ----------------------------------------------------------------------------
483481
#ifdef DEBUG_CRASHING
484482
/**
485-
Print a character string to the logfile and/or console, then halt execution
483+
Print a character string to the log file and/or console, then halt execution
486484
while presenting the user with an exit/debug/ignore dialog containing the same
487485
text message.
486+
487+
TheSuperHackers @tweak Now shows a message box without any logging when debug was not yet initialized.
488488
*/
489489
void DebugCrash(const char *format, ...)
490490
{
@@ -493,53 +493,40 @@ void DebugCrash(const char *format, ...)
493493

494494
// make it not static so that it'll be thread-safe.
495495
// make it big to avoid weird overflow bugs in debug mode
496-
char theCrashBuffer[ LARGE_BUFFER ];
497-
if (theDebugFlags == 0)
498-
{
499-
if (!DX8Wrapper_IsWindowed) {
500-
if (ApplicationHWnd) {
501-
ShowWindow(ApplicationHWnd, SW_HIDE);
502-
}
503-
}
504-
MessageBoxWrapper("DebugCrash - Debug not inited properly", "", MB_OK|MB_TASKMODAL);
505-
}
496+
char theCrashBuffer[ LARGE_BUFFER ];
506497

507498
prepBuffer(theCrashBuffer);
508499
strcat(theCrashBuffer, "ASSERTION FAILURE: ");
509500

510501
va_list arg;
511502
va_start(arg, format);
512-
vsprintf(theCrashBuffer + strlen(theCrashBuffer), format, arg);
503+
vsnprintf(theCrashBuffer + strlen(theCrashBuffer), LARGE_BUFFER, format, arg);
513504
va_end(arg);
514505

515-
if (strlen(theCrashBuffer) >= sizeof(theCrashBuffer))
516-
{
517-
if (!DX8Wrapper_IsWindowed) {
518-
if (ApplicationHWnd) {
519-
ShowWindow(ApplicationHWnd, SW_HIDE);
520-
}
521-
}
522-
MessageBoxWrapper("String too long for debug buffers", "", MB_OK|MB_TASKMODAL);
523-
}
506+
whackFunnyCharacters(theCrashBuffer);
524507

525-
#ifdef DEBUG_LOGGING
526-
if (ignoringAsserts())
508+
const bool useLogging = theDebugFlags != 0;
509+
510+
if (useLogging)
527511
{
528-
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
529-
}
530-
whackFunnyCharacters(theCrashBuffer);
531-
doLogOutput(theCrashBuffer);
512+
#ifdef DEBUG_LOGGING
513+
if (ignoringAsserts())
514+
{
515+
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
516+
}
517+
doLogOutput(theCrashBuffer);
532518
#endif
533519
#ifdef DEBUG_STACKTRACE
534-
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
535-
{
536-
doStackDump();
537-
}
520+
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
521+
{
522+
doStackDump();
523+
}
538524
#endif
525+
}
539526

540527
strcat(theCrashBuffer, "\n\nAbort->exception; Retry->debugger; Ignore->continue");
541528

542-
int result = doCrashBox(theCrashBuffer, true);
529+
const int result = doCrashBox(theCrashBuffer, useLogging);
543530

544531
if (result == IDIGNORE && TheCurrentIgnoreCrashPtr != NULL)
545532
{

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
124124
s_initFillerValue |= (~(s_initFillerValue << 4)) & 0xf0;
125125
s_initFillerValue |= (s_initFillerValue << 8);
126126
s_initFillerValue |= (s_initFillerValue << 16);
127-
DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
127+
//DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
128128
}
129129
#endif
130130

@@ -3424,6 +3424,9 @@ void initMemoryManager()
34243424
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
34253425
userMemoryManagerInitPools();
34263426
thePreMainInitFlag = false;
3427+
3428+
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3429+
DEBUG_LOG(("*** Initialized the Memory Manager"));
34273430
}
34283431
else
34293432
{
@@ -3433,7 +3436,7 @@ void initMemoryManager()
34333436
}
34343437
else
34353438
{
3436-
DEBUG_CRASH(("memory manager is already inited"));
3439+
DEBUG_CRASH(("Memory Manager is already initialized"));
34373440
}
34383441
}
34393442

@@ -3487,8 +3490,6 @@ static void preMainInitMemoryManager()
34873490
{
34883491
if (TheMemoryPoolFactory == NULL)
34893492
{
3490-
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3491-
DEBUG_LOG(("*** Initing Memory Manager prior to main!"));
34923493

34933494
Int numSubPools;
34943495
const PoolInitRec *pParms;
@@ -3499,6 +3500,9 @@ static void preMainInitMemoryManager()
34993500
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
35003501
userMemoryManagerInitPools();
35013502
thePreMainInitFlag = true;
3503+
3504+
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3505+
DEBUG_LOG(("*** Initialized the Memory Manager prior to main!"));
35023506
}
35033507
}
35043508

@@ -3543,6 +3547,8 @@ void shutdownMemoryManager()
35433547
}
35443548

35453549
theMainInitFlag = false;
3550+
3551+
DEBUG_SHUTDOWN();
35463552
}
35473553

35483554
//-----------------------------------------------------------------------------

Generals/Code/Main/WinMain.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
768768
}
769769
::SetCurrentDirectory(buffer);
770770

771-
CommandLine::parseCommandLineForStartup();
772771

773772
#ifdef RTS_DEBUG
774773
// Turn on Memory heap tracking
@@ -788,6 +787,11 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
788787
// Force to be loaded from a file, not a resource so same exe can be used in germany and retail.
789788
gLoadScreenBitmap = (HBITMAP)LoadImage(hInstance, "Install_Final.bmp", IMAGE_BITMAP, 0, 0, LR_SHARED|LR_LOADFROMFILE);
790789

790+
// initialize the memory manager early
791+
initMemoryManager();
792+
793+
CommandLine::parseCommandLineForStartup();
794+
791795
// register windows class and create application window
792796
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
793797
return exitcode;
@@ -804,9 +808,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
804808
// BGC - initialize COM
805809
// OleInitialize(NULL);
806810

807-
// start the log
808-
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
809-
initMemoryManager();
810811

811812

812813
// Set up version info
@@ -830,7 +831,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
830831
delete TheVersion;
831832
TheVersion = NULL;
832833
shutdownMemoryManager();
833-
DEBUG_SHUTDOWN();
834834
return exitcode;
835835
}
836836
DEBUG_LOG(("Create Generals Mutex okay."));
@@ -849,9 +849,7 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
849849
TheMemoryPoolFactory->memoryPoolUsageReport("AAAMemStats");
850850
#endif
851851

852-
// close the log
853852
shutdownMemoryManager();
854-
DEBUG_SHUTDOWN();
855853

856854
// BGC - shut down COM
857855
// OleUninitialize();

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

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ inline HWND getThreadHWND()
167167
int MessageBoxWrapper( LPCSTR lpText, LPCSTR lpCaption, UINT uType )
168168
{
169169
HWND threadHWND = getThreadHWND();
170-
if (!threadHWND)
171-
return (uType & MB_ABORTRETRYIGNORE)?IDIGNORE:IDYES;
172-
173170
return ::MessageBox(threadHWND, lpText, lpCaption, uType);
174171
}
175172

@@ -366,8 +363,9 @@ void DebugInit(int flags)
366363
#ifdef DEBUG_LOGGING
367364

368365
// TheSuperHackers @info Debug initialization can happen very early.
369-
// Therefore, parse initial commandline and initialize the client instance now.
366+
// Determine the client instance id before creating the log file with an instance specific name.
370367
CommandLine::parseCommandLineForStartup();
368+
371369
if (!rts::ClientInstance::initialize())
372370
return;
373371

@@ -483,9 +481,11 @@ const char* DebugGetLogFileNamePrev()
483481
// ----------------------------------------------------------------------------
484482
#ifdef DEBUG_CRASHING
485483
/**
486-
Print a character string to the logfile and/or console, then halt execution
484+
Print a character string to the log file and/or console, then halt execution
487485
while presenting the user with an exit/debug/ignore dialog containing the same
488486
text message.
487+
488+
TheSuperHackers @tweak Now shows a message box without any logging when debug was not yet initialized.
489489
*/
490490
void DebugCrash(const char *format, ...)
491491
{
@@ -494,53 +494,40 @@ void DebugCrash(const char *format, ...)
494494

495495
// make it not static so that it'll be thread-safe.
496496
// make it big to avoid weird overflow bugs in debug mode
497-
char theCrashBuffer[ LARGE_BUFFER ];
498-
if (theDebugFlags == 0)
499-
{
500-
if (!DX8Wrapper_IsWindowed) {
501-
if (ApplicationHWnd) {
502-
ShowWindow(ApplicationHWnd, SW_HIDE);
503-
}
504-
}
505-
MessageBoxWrapper("DebugCrash - Debug not inited properly", "", MB_OK|MB_TASKMODAL);
506-
}
497+
char theCrashBuffer[ LARGE_BUFFER ];
507498

508499
prepBuffer(theCrashBuffer);
509500
strcat(theCrashBuffer, "ASSERTION FAILURE: ");
510501

511502
va_list arg;
512503
va_start(arg, format);
513-
vsprintf(theCrashBuffer + strlen(theCrashBuffer), format, arg);
504+
vsnprintf(theCrashBuffer + strlen(theCrashBuffer), LARGE_BUFFER, format, arg);
514505
va_end(arg);
515506

516-
if (strlen(theCrashBuffer) >= sizeof(theCrashBuffer))
517-
{
518-
if (!DX8Wrapper_IsWindowed) {
519-
if (ApplicationHWnd) {
520-
ShowWindow(ApplicationHWnd, SW_HIDE);
521-
}
522-
}
523-
MessageBoxWrapper("String too long for debug buffers", "", MB_OK|MB_TASKMODAL);
524-
}
507+
whackFunnyCharacters(theCrashBuffer);
525508

526-
#ifdef DEBUG_LOGGING
527-
if (ignoringAsserts())
509+
const bool useLogging = theDebugFlags != 0;
510+
511+
if (useLogging)
528512
{
529-
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
530-
}
531-
whackFunnyCharacters(theCrashBuffer);
532-
doLogOutput(theCrashBuffer);
513+
#ifdef DEBUG_LOGGING
514+
if (ignoringAsserts())
515+
{
516+
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
517+
}
518+
doLogOutput(theCrashBuffer);
533519
#endif
534520
#ifdef DEBUG_STACKTRACE
535-
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
536-
{
537-
doStackDump();
538-
}
521+
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
522+
{
523+
doStackDump();
524+
}
539525
#endif
526+
}
540527

541528
strcat(theCrashBuffer, "\n\nAbort->exception; Retry->debugger; Ignore->continue");
542529

543-
int result = doCrashBox(theCrashBuffer, true);
530+
const int result = doCrashBox(theCrashBuffer, useLogging);
544531

545532
if (result == IDIGNORE && TheCurrentIgnoreCrashPtr != NULL)
546533
{

GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
126126
s_initFillerValue |= (~(s_initFillerValue << 4)) & 0xf0;
127127
s_initFillerValue |= (s_initFillerValue << 8);
128128
s_initFillerValue |= (s_initFillerValue << 16);
129-
DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
129+
//DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
130130
}
131131
#endif
132132

@@ -3414,6 +3414,9 @@ void initMemoryManager()
34143414
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
34153415
userMemoryManagerInitPools();
34163416
thePreMainInitFlag = false;
3417+
3418+
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3419+
DEBUG_LOG(("*** Initialized the Memory Manager"));
34173420
}
34183421
else
34193422
{
@@ -3423,7 +3426,7 @@ void initMemoryManager()
34233426
}
34243427
else
34253428
{
3426-
DEBUG_CRASH(("memory manager is already inited"));
3429+
DEBUG_CRASH(("Memory Manager is already initialized"));
34273430
}
34283431
}
34293432

@@ -3477,8 +3480,6 @@ static void preMainInitMemoryManager()
34773480
{
34783481
if (TheMemoryPoolFactory == NULL)
34793482
{
3480-
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3481-
DEBUG_LOG(("*** Initing Memory Manager prior to main!"));
34823483

34833484
Int numSubPools;
34843485
const PoolInitRec *pParms;
@@ -3489,6 +3490,9 @@ static void preMainInitMemoryManager()
34893490
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
34903491
userMemoryManagerInitPools();
34913492
thePreMainInitFlag = true;
3493+
3494+
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
3495+
DEBUG_LOG(("*** Initialized the Memory Manager prior to main!"));
34923496
}
34933497
}
34943498

@@ -3533,6 +3537,8 @@ void shutdownMemoryManager()
35333537
}
35343538

35353539
theMainInitFlag = false;
3540+
3541+
DEBUG_SHUTDOWN();
35363542
}
35373543

35383544
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)