Skip to content

[GEN][ZH] Fix Memory Manager initialization issues #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletions Generals/Code/GameEngine/Source/Common/System/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ inline HWND getThreadHWND()
int MessageBoxWrapper( LPCSTR lpText, LPCSTR lpCaption, UINT uType )
{
HWND threadHWND = getThreadHWND();
if (!threadHWND)
return (uType & MB_ABORTRETRYIGNORE)?IDIGNORE:IDYES;

return ::MessageBox(threadHWND, lpText, lpCaption, uType);
}

Expand Down Expand Up @@ -365,8 +362,9 @@ void DebugInit(int flags)
#ifdef DEBUG_LOGGING

// TheSuperHackers @info Debug initialization can happen very early.
// Therefore, parse initial commandline and initialize the client instance now.
// Determine the client instance id before creating the log file with an instance specific name.
CommandLine::parseCommandLineForStartup();

if (!rts::ClientInstance::initialize())
return;

Expand Down Expand Up @@ -482,9 +480,11 @@ const char* DebugGetLogFileNamePrev()
// ----------------------------------------------------------------------------
#ifdef DEBUG_CRASHING
/**
Print a character string to the logfile and/or console, then halt execution
Print a character string to the log file and/or console, then halt execution
while presenting the user with an exit/debug/ignore dialog containing the same
text message.
TheSuperHackers @tweak Now shows a message box without any logging when debug was not yet initialized.
*/
void DebugCrash(const char *format, ...)
{
Expand All @@ -493,53 +493,40 @@ void DebugCrash(const char *format, ...)

// make it not static so that it'll be thread-safe.
// make it big to avoid weird overflow bugs in debug mode
char theCrashBuffer[ LARGE_BUFFER ];
if (theDebugFlags == 0)
{
if (!DX8Wrapper_IsWindowed) {
if (ApplicationHWnd) {
ShowWindow(ApplicationHWnd, SW_HIDE);
}
}
MessageBoxWrapper("DebugCrash - Debug not inited properly", "", MB_OK|MB_TASKMODAL);
}
char theCrashBuffer[ LARGE_BUFFER ];

prepBuffer(theCrashBuffer);
strcat(theCrashBuffer, "ASSERTION FAILURE: ");

va_list arg;
va_start(arg, format);
vsprintf(theCrashBuffer + strlen(theCrashBuffer), format, arg);
vsnprintf(theCrashBuffer + strlen(theCrashBuffer), LARGE_BUFFER, format, arg);
va_end(arg);

if (strlen(theCrashBuffer) >= sizeof(theCrashBuffer))
{
if (!DX8Wrapper_IsWindowed) {
if (ApplicationHWnd) {
ShowWindow(ApplicationHWnd, SW_HIDE);
}
}
MessageBoxWrapper("String too long for debug buffers", "", MB_OK|MB_TASKMODAL);
}
whackFunnyCharacters(theCrashBuffer);

#ifdef DEBUG_LOGGING
if (ignoringAsserts())
const bool useLogging = theDebugFlags != 0;

if (useLogging)
{
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
}
whackFunnyCharacters(theCrashBuffer);
doLogOutput(theCrashBuffer);
#ifdef DEBUG_LOGGING
if (ignoringAsserts())
{
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
}
doLogOutput(theCrashBuffer);
#endif
#ifdef DEBUG_STACKTRACE
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
{
doStackDump();
}
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
{
doStackDump();
}
#endif
}

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

int result = doCrashBox(theCrashBuffer, true);
const int result = doCrashBox(theCrashBuffer, useLogging);

if (result == IDIGNORE && TheCurrentIgnoreCrashPtr != NULL)
{
Expand Down
14 changes: 10 additions & 4 deletions Generals/Code/GameEngine/Source/Common/System/GameMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
s_initFillerValue |= (~(s_initFillerValue << 4)) & 0xf0;
s_initFillerValue |= (s_initFillerValue << 8);
s_initFillerValue |= (s_initFillerValue << 16);
DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
//DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
}
#endif

Expand Down Expand Up @@ -3424,6 +3424,9 @@ void initMemoryManager()
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
userMemoryManagerInitPools();
thePreMainInitFlag = false;

DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initialized the Memory Manager"));
}
else
{
Expand All @@ -3433,7 +3436,7 @@ void initMemoryManager()
}
else
{
DEBUG_CRASH(("memory manager is already inited"));
DEBUG_CRASH(("Memory Manager is already initialized"));
}
}

Expand Down Expand Up @@ -3487,8 +3490,6 @@ static void preMainInitMemoryManager()
{
if (TheMemoryPoolFactory == NULL)
{
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initing Memory Manager prior to main!"));

Int numSubPools;
const PoolInitRec *pParms;
Expand All @@ -3499,6 +3500,9 @@ static void preMainInitMemoryManager()
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
userMemoryManagerInitPools();
thePreMainInitFlag = true;

DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initialized the Memory Manager prior to main!"));
}
}

Expand Down Expand Up @@ -3543,6 +3547,8 @@ void shutdownMemoryManager()
}

theMainInitFlag = false;

DEBUG_SHUTDOWN();
}

//-----------------------------------------------------------------------------
Expand Down
12 changes: 5 additions & 7 deletions Generals/Code/Main/WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
}
::SetCurrentDirectory(buffer);

CommandLine::parseCommandLineForStartup();

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

// initialize the memory manager early
initMemoryManager();

CommandLine::parseCommandLineForStartup();

// register windows class and create application window
if(!TheGlobalData->m_headless && initializeAppWindows(hInstance, nCmdShow, TheGlobalData->m_windowed) == false)
return exitcode;
Expand All @@ -804,9 +808,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
// BGC - initialize COM
// OleInitialize(NULL);

// start the log
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
initMemoryManager();


// Set up version info
Expand All @@ -830,7 +831,6 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
delete TheVersion;
TheVersion = NULL;
shutdownMemoryManager();
DEBUG_SHUTDOWN();
return exitcode;
}
DEBUG_LOG(("Create Generals Mutex okay."));
Expand All @@ -849,9 +849,7 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
TheMemoryPoolFactory->memoryPoolUsageReport("AAAMemStats");
#endif

// close the log
shutdownMemoryManager();
DEBUG_SHUTDOWN();

// BGC - shut down COM
// OleUninitialize();
Expand Down
59 changes: 23 additions & 36 deletions GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ inline HWND getThreadHWND()
int MessageBoxWrapper( LPCSTR lpText, LPCSTR lpCaption, UINT uType )
{
HWND threadHWND = getThreadHWND();
if (!threadHWND)
return (uType & MB_ABORTRETRYIGNORE)?IDIGNORE:IDYES;

return ::MessageBox(threadHWND, lpText, lpCaption, uType);
}

Expand Down Expand Up @@ -366,8 +363,9 @@ void DebugInit(int flags)
#ifdef DEBUG_LOGGING

// TheSuperHackers @info Debug initialization can happen very early.
// Therefore, parse initial commandline and initialize the client instance now.
// Determine the client instance id before creating the log file with an instance specific name.
CommandLine::parseCommandLineForStartup();

if (!rts::ClientInstance::initialize())
return;

Expand Down Expand Up @@ -483,9 +481,11 @@ const char* DebugGetLogFileNamePrev()
// ----------------------------------------------------------------------------
#ifdef DEBUG_CRASHING
/**
Print a character string to the logfile and/or console, then halt execution
Print a character string to the log file and/or console, then halt execution
while presenting the user with an exit/debug/ignore dialog containing the same
text message.

TheSuperHackers @tweak Now shows a message box without any logging when debug was not yet initialized.
*/
void DebugCrash(const char *format, ...)
{
Expand All @@ -494,53 +494,40 @@ void DebugCrash(const char *format, ...)

// make it not static so that it'll be thread-safe.
// make it big to avoid weird overflow bugs in debug mode
char theCrashBuffer[ LARGE_BUFFER ];
if (theDebugFlags == 0)
{
if (!DX8Wrapper_IsWindowed) {
if (ApplicationHWnd) {
ShowWindow(ApplicationHWnd, SW_HIDE);
}
}
MessageBoxWrapper("DebugCrash - Debug not inited properly", "", MB_OK|MB_TASKMODAL);
}
char theCrashBuffer[ LARGE_BUFFER ];

prepBuffer(theCrashBuffer);
strcat(theCrashBuffer, "ASSERTION FAILURE: ");

va_list arg;
va_start(arg, format);
vsprintf(theCrashBuffer + strlen(theCrashBuffer), format, arg);
vsnprintf(theCrashBuffer + strlen(theCrashBuffer), LARGE_BUFFER, format, arg);
va_end(arg);

if (strlen(theCrashBuffer) >= sizeof(theCrashBuffer))
{
if (!DX8Wrapper_IsWindowed) {
if (ApplicationHWnd) {
ShowWindow(ApplicationHWnd, SW_HIDE);
}
}
MessageBoxWrapper("String too long for debug buffers", "", MB_OK|MB_TASKMODAL);
}
whackFunnyCharacters(theCrashBuffer);

#ifdef DEBUG_LOGGING
if (ignoringAsserts())
const bool useLogging = theDebugFlags != 0;

if (useLogging)
{
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
}
whackFunnyCharacters(theCrashBuffer);
doLogOutput(theCrashBuffer);
#ifdef DEBUG_LOGGING
if (ignoringAsserts())
{
doLogOutput("**** CRASH IN FULL SCREEN - Auto-ignored, CHECK THIS LOG!");
}
doLogOutput(theCrashBuffer);
#endif
#ifdef DEBUG_STACKTRACE
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
{
doStackDump();
}
if (!(TheGlobalData && TheGlobalData->m_debugIgnoreStackTrace))
{
doStackDump();
}
#endif
}

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

int result = doCrashBox(theCrashBuffer, true);
const int result = doCrashBox(theCrashBuffer, useLogging);

if (result == IDIGNORE && TheCurrentIgnoreCrashPtr != NULL)
{
Expand Down
14 changes: 10 additions & 4 deletions GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
s_initFillerValue |= (~(s_initFillerValue << 4)) & 0xf0;
s_initFillerValue |= (s_initFillerValue << 8);
s_initFillerValue |= (s_initFillerValue << 16);
DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
//DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
}
#endif

Expand Down Expand Up @@ -3414,6 +3414,9 @@ void initMemoryManager()
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
userMemoryManagerInitPools();
thePreMainInitFlag = false;

DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initialized the Memory Manager"));
}
else
{
Expand All @@ -3423,7 +3426,7 @@ void initMemoryManager()
}
else
{
DEBUG_CRASH(("memory manager is already inited"));
DEBUG_CRASH(("Memory Manager is already initialized"));
}
}

Expand Down Expand Up @@ -3477,8 +3480,6 @@ static void preMainInitMemoryManager()
{
if (TheMemoryPoolFactory == NULL)
{
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initing Memory Manager prior to main!"));

Int numSubPools;
const PoolInitRec *pParms;
Expand All @@ -3489,6 +3490,9 @@ static void preMainInitMemoryManager()
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
userMemoryManagerInitPools();
thePreMainInitFlag = true;

DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
DEBUG_LOG(("*** Initialized the Memory Manager prior to main!"));
}
}

Expand Down Expand Up @@ -3533,6 +3537,8 @@ void shutdownMemoryManager()
}

theMainInitFlag = false;

DEBUG_SHUTDOWN();
}

//-----------------------------------------------------------------------------
Expand Down
Loading
Loading