Skip to content

Commit 4247d47

Browse files
committed
[ZH] Fix Memory Manager initialization issues
1 parent e5110b6 commit 4247d47

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

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(("*** Intialized 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
//-----------------------------------------------------------------------------

GeneralsMD/Code/Main/WinMain.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
794794
}
795795
::SetCurrentDirectory(buffer);
796796

797-
CommandLine::parseCommandLineForStartup();
798797

799798
#ifdef RTS_DEBUG
800799
// Turn on Memory heap tracking
@@ -834,6 +833,11 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
834833
gLoadScreenBitmap = (HBITMAP)LoadImage(hInstance, "Install_Final.bmp", IMAGE_BITMAP, 0, 0, LR_SHARED|LR_LOADFROMFILE);
835834
#endif
836835

836+
// initialize the memory manager early
837+
initMemoryManager();
838+
839+
CommandLine::parseCommandLineForStartup();
840+
837841
// register windows class and create application window
838842
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
839843
return exitcode;
@@ -850,9 +854,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
850854
// BGC - initialize COM
851855
// OleInitialize(NULL);
852856

853-
// start the log
854-
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
855-
initMemoryManager();
856857

857858

858859
// Set up version info
@@ -876,7 +877,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
876877
delete TheVersion;
877878
TheVersion = NULL;
878879
shutdownMemoryManager();
879-
DEBUG_SHUTDOWN();
880880
return exitcode;
881881
}
882882
DEBUG_LOG(("Create Generals Mutex okay."));
@@ -896,9 +896,7 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
896896
TheMemoryPoolFactory->memoryPoolUsageReport("AAAMemStats");
897897
#endif
898898

899-
// close the log
900899
shutdownMemoryManager();
901-
DEBUG_SHUTDOWN();
902900

903901
// BGC - shut down COM
904902
// OleUninitialize();

0 commit comments

Comments
 (0)