We have a rare crash in a 32bit C++ executable on Windows that doesn't produce a memory dump. We don't have a repro case and when the crash happens the game just disappears after a few seconds, so we need a memory dump to find the cause. Other crashes do produce a memory dump, so we know that our memory dump function sometimes works, just not on this particular crash.
Under what circumstances does the combination of _set_se_translator
and MiniDumpWriteDump
fail to produce a memory dump? Are we doing something wrong in our memory dump handling that causes it to sometimes not produce a dump?
Here's what we currently do:
At the start of the main function in the main thread we call SetUnhandledExceptionFilter(CrashDumpManager::unhandledExceptionHandler);
In every thread we call _set_se_translator(CrashDumpManager::MiniDumpFunction);
This is what CrashDumpManager.h looks like:
#include <Windows.h>
#include <Dbghelp.h>
class CrashDumpManager
{
public:
static void MiniDumpFunction(unsigned int nExceptionCode, EXCEPTION_POINTERS *pException);
static LONG CALLBACK unhandledExceptionHandler(EXCEPTION_POINTERS* e);
//If the game crashes because of a memory leak then there won't be enough memory free to generate a memory dump
//Therefore 10MB is allocated here and deleted before the crashdump is written.
static unsigned char* crashdumpMemory;
};
And this is what CrashDumpManager.cpp looks like:
#include "CrashDumpManager.h"
void CrashDumpManager::MiniDumpFunction(unsigned int nExceptionCode, EXCEPTION_POINTERS *pException)
{
delete crashdumpMemory;
crashdumpMemory = nullptr;
// prevent stack overflow when crashing in this function
static bool calledFunctionOnce = false;
if (!calledFunctionOnce)
{
calledFunctionOnce = true;
HMODULE dbgHelpModule = LoadLibraryA("dbghelp");
if (dbgHelpModule == nullptr)
return;
auto writeMiniDumpFunction = (decltype(&MiniDumpWriteDump))GetProcAddress(dbgHelpModule, "MiniDumpWriteDump");
if (writeMiniDumpFunction == nullptr)
return;
char name[MAX_PATH];
{
char* nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH);
SYSTEMTIME t;
GetSystemTime(&t);
wsprintfA(nameEnd - strlen(".exe"), "_%4d%02d%02d_%02d%02d%02d.mdmp",
t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
}
HANDLE dumpFileHandle = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (dumpFileHandle == INVALID_HANDLE_VALUE)
return;
MINIDUMP_EXCEPTION_INFORMATION exceptionInfo;
exceptionInfo.ThreadId = GetCurrentThreadId();
exceptionInfo.ExceptionPointers = pException;
exceptionInfo.ClientPointers = FALSE;
auto dumped = writeMiniDumpFunction(GetCurrentProcess(), GetCurrentProcessId(), dumpFileHandle,
MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory),
pException ? &exceptionInfo : nullptr, nullptr, nullptr);
CloseHandle(dumpFileHandle);
}
}
LONG CALLBACK CrashDumpManager::unhandledExceptionHandler(EXCEPTION_POINTERS* e)
{
CrashDumpManager::MiniDumpFunction(0, e);
return EXCEPTION_CONTINUE_SEARCH;
}
unsigned char* CrashDumpManager::crashdumpMemory = new unsigned char[10*1024*1024];