Skip to content

Commit d1a1048

Browse files
committed
Simplified shutdown and cleanup
1 parent c4398cf commit d1a1048

File tree

5 files changed

+51
-54
lines changed

5 files changed

+51
-54
lines changed

Core/GameEngine/Include/Common/MiniDumper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class MiniDumper
5757
BOOL DumpMemoryObjects(ULONG64& memoryBase, ULONG& memorySize);
5858
void CleanupResources();
5959
Bool IsDumpThreadStillRunning() const;
60+
void ShutdownDumpThread();
6061

6162
// Callbacks from dbghelp
6263
static BOOL CALLBACK MiniDumpCallback(PVOID CallbackParam, PMINIDUMP_CALLBACK_INPUT CallbackInput, PMINIDUMP_CALLBACK_OUTPUT CallbackOutput);

Core/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,9 @@ void ReleaseCrash(const char *reason)
734734
// Do dumps both with and without extended info
735735
TheMiniDumper->TriggerMiniDump(DUMP_TYPE_MINIMAL);
736736
TheMiniDumper->TriggerMiniDump(DUMP_TYPE_GAMEMEMORY);
737-
MiniDumper::shutdownMiniDumper();
738737
}
738+
739+
MiniDumper::shutdownMiniDumper();
739740
#endif
740741

741742
char prevbuf[ _MAX_PATH ];
@@ -809,8 +810,9 @@ void ReleaseCrashLocalized(const AsciiString& p, const AsciiString& m)
809810
// Do dumps both with and without extended info
810811
TheMiniDumper->TriggerMiniDump(DUMP_TYPE_MINIMAL);
811812
TheMiniDumper->TriggerMiniDump(DUMP_TYPE_GAMEMEMORY);
812-
MiniDumper::shutdownMiniDumper();
813813
}
814+
815+
MiniDumper::shutdownMiniDumper();
814816
#endif
815817

816818
UnicodeString prompt = TheGameText->fetch(p);

Core/GameEngine/Source/Common/System/MiniDumper.cpp

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,19 @@ void MiniDumper::Initialize(const AsciiString& userDirPath)
176176
{
177177
// Something went wrong with the creation of the events..
178178
DEBUG_LOG(("MiniDumper::Initialize: Unable to create events: error=%u", ::GetLastError()));
179-
CleanupResources();
180179
return;
181180
}
182181

183182
m_dumpThread = ::CreateThread(NULL, 0, MiniDumpThreadProc, this, CREATE_SUSPENDED, &m_dumpThreadId);
184183
if (!m_dumpThread)
185184
{
186185
DEBUG_LOG(("MiniDumper::Initialize: Unable to create thread: error=%u", ::GetLastError()));
187-
CleanupResources();
188186
return;
189187
}
190188

191-
if (!::ResumeThread(m_dumpThread))
189+
if (::ResumeThread(m_dumpThread) != 1)
192190
{
193191
DEBUG_LOG(("MiniDumper::Initialize: Unable to resume thread: error=%u", ::GetLastError()));
194-
CleanupResources();
195192
return;
196193
}
197194

@@ -207,7 +204,7 @@ Bool MiniDumper::IsInitialized() const
207204
Bool MiniDumper::IsDumpThreadStillRunning() const
208205
{
209206
DWORD exitCode;
210-
if (::GetExitCodeThread(m_dumpThread, &exitCode) && exitCode == STILL_ACTIVE)
207+
if (m_dumpThread != NULL && ::GetExitCodeThread(m_dumpThread, &exitCode) && exitCode == STILL_ACTIVE)
211208
{
212209
return true;
213210
}
@@ -238,15 +235,50 @@ Bool MiniDumper::InitializeDumpDirectory(const AsciiString& userDirPath)
238235
return true;
239236
}
240237

241-
void MiniDumper::CleanupResources()
238+
void MiniDumper::ShutdownDumpThread()
242239
{
240+
if (IsDumpThreadStillRunning())
241+
{
242+
DEBUG_ASSERTCRASH(m_quitting != NULL, ("MiniDumper::ShutdownDumpThread: Dump thread still running despite m_quitting being NULL"));
243+
::SetEvent(m_quitting);
244+
245+
DWORD waitRet = ::WaitForSingleObject(m_dumpThread, 3000);
246+
if (waitRet != WAIT_OBJECT_0)
247+
{
248+
if (waitRet == WAIT_TIMEOUT)
249+
{
250+
DEBUG_LOG(("MiniDumper::ShutdownDumpThread: Waiting for dumping thread to exit timed out, killing thread", waitRet));
251+
::TerminateThread(m_dumpThread, DUMPER_EXIT_FORCED_TERMINATE);
252+
}
253+
else if (waitRet == WAIT_FAILED)
254+
{
255+
DEBUG_LOG(("MiniDumper::ShutdownDumpThread: Waiting for minidump triggering failed: status=%u, error=%u", waitRet, ::GetLastError()));
256+
}
257+
else
258+
{
259+
DEBUG_LOG(("MiniDumper::ShutdownDumpThread: Waiting for minidump triggering failed: status=%u", waitRet));
260+
}
261+
}
262+
}
263+
}
264+
265+
void MiniDumper::ShutDown()
266+
{
267+
ShutdownDumpThread();
268+
243269
if (m_dumpThread != NULL)
244270
{
245-
DEBUG_ASSERTCRASH(!IsDumpThreadStillRunning(), ("MiniDumper::CleanupResources() called while Dump thread still active"));
271+
DEBUG_ASSERTCRASH(!IsDumpThreadStillRunning(), ("MiniDumper::ShutDown: ShutdownDumpThread() was unable to stop Dump thread"));
246272
::CloseHandle(m_dumpThread);
247273
m_dumpThread = NULL;
248274
}
249275

276+
if (m_quitting != NULL)
277+
{
278+
::CloseHandle(m_quitting);
279+
m_quitting = NULL;
280+
}
281+
250282
if (m_dumpComplete != NULL)
251283
{
252284
::CloseHandle(m_dumpComplete);
@@ -259,52 +291,12 @@ void MiniDumper::CleanupResources()
259291
m_dumpRequested = NULL;
260292
}
261293

262-
if (m_quitting != NULL)
294+
if (m_loadedDbgHelp)
263295
{
264-
::CloseHandle(m_quitting);
265-
m_quitting = NULL;
266-
}
267-
268-
DbgHelpLoader::unload();
269-
m_loadedDbgHelp = false;
270-
}
271-
272-
void MiniDumper::ShutDown()
273-
{
274-
if (!m_miniDumpInitialized)
275-
{
276-
// Even if we failed to initialize, DbgHelpLoader could still have loaded its library
277-
if (m_loadedDbgHelp)
278-
{
279-
DbgHelpLoader::unload();
280-
m_loadedDbgHelp = false;
281-
}
282-
283-
return;
284-
}
285-
286-
::SetEvent(m_quitting);
287-
DWORD waitRet = ::WaitForSingleObject(m_dumpThread, 3000);
288-
if (waitRet != WAIT_OBJECT_0)
289-
{
290-
if (waitRet == WAIT_TIMEOUT)
291-
{
292-
DEBUG_LOG(("MiniDumper::ShutDown: Waiting for dumping thread to exit timed out, killing thread", waitRet));
293-
::TerminateThread(m_dumpThread, DUMPER_EXIT_FORCED_TERMINATE);
294-
}
295-
else if (waitRet == WAIT_FAILED)
296-
{
297-
DEBUG_LOG(("MiniDumper::ShutDown: Waiting for minidump triggering failed: status=%u, error=%u", waitRet, ::GetLastError()));
298-
}
299-
else
300-
{
301-
DEBUG_LOG(("MiniDumper::ShutDown: Waiting for minidump triggering failed: status=%u", waitRet));
302-
}
303-
304-
return;
296+
DbgHelpLoader::unload();
297+
m_loadedDbgHelp = false;
305298
}
306299

307-
CleanupResources();
308300
m_miniDumpInitialized = false;
309301
}
310302

Generals/Code/Main/WinMain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,9 @@ static LONG WINAPI UnHandledExceptionFilter( struct _EXCEPTION_POINTERS* e_info
750750
// Do dumps both with and without extended info
751751
TheMiniDumper->TriggerMiniDumpForException(e_info, DUMP_TYPE_MINIMAL);
752752
TheMiniDumper->TriggerMiniDumpForException(e_info, DUMP_TYPE_GAMEMEMORY);
753-
MiniDumper::shutdownMiniDumper();
754753
}
754+
755+
MiniDumper::shutdownMiniDumper();
755756
#endif
756757
return EXCEPTION_EXECUTE_HANDLER;
757758
}

GeneralsMD/Code/Main/WinMain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,9 @@ static LONG WINAPI UnHandledExceptionFilter( struct _EXCEPTION_POINTERS* e_info
772772
// Do dumps both with and without extended info
773773
TheMiniDumper->TriggerMiniDumpForException(e_info, DUMP_TYPE_MINIMAL);
774774
TheMiniDumper->TriggerMiniDumpForException(e_info, DUMP_TYPE_GAMEMEMORY);
775-
MiniDumper::shutdownMiniDumper();
776775
}
776+
777+
MiniDumper::shutdownMiniDumper();
777778
#endif
778779
return EXCEPTION_EXECUTE_HANDLER;
779780
}

0 commit comments

Comments
 (0)