In Windows, I have some threads. Two of them terminate with exception (null pointer dereference, for example). I have SetUnhandledExceptionFilter(...)
which starts dump generating on the first exception. On the second exception, the whole program dies. Is there any way to handle such situations? All critical errors except first shall be ignored.
pseudo code:
void job()
{
...
RaiseException(someCode, someFlags, 0, nullptr); // or doing something wrong, like nullptr dereference
}
int main() {
SetUnhandledExceptionFilter(getDump);
std::thread t1(job), t2(job);
...
}
UPD: replace misunderstanded string *nullptr = 0xbad;
UPD2: forget about nullptr
UPD3: so far i came to this workaround
#include <stdio.h>
#include <windows.h> // for EXCEPTION_ACCESS_VIOLATION
#include <excpt.h>
#include <mutex>
LONG __stdcall HandleException(EXCEPTION_POINTERS* exinfo)
{
static HANDLE mutex = CreateMutex(nullptr, FALSE, __TEXT("HandleException"));
while(WaitForSingleObject(mutex, INFINITE) != WAIT_OBJECT_0);
HANDLE event = CreateEvent(nullptr, TRUE, FALSE, __TEXT("Doing Dump"));
puts("Doing Dump");
WaitForSingleObject(event, 5000); // do dump routine
puts("Done Dump");
return EXCEPTION_EXECUTE_HANDLER;
}
int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep)
{
puts("in filter.");
return HandleException(ep);
}
void Job()
{
puts("hello");
int *p = 0x00000000; // pointer to NULL
*p = 13; // causes an access violation exception;
}
void safeJob(void (*job)())
{
__try {
job();
} __except (filter(GetExceptionCode(), GetExceptionInformation())) {
exit(-1);
}
}
int main()
{
SetUnhandledExceptionFilter(HandleException);
std::thread t1(std::bind(safeJob, Job));
std::thread t2(std::bind(safeJob, Job));
t1.join();
t2.join();
return 0;
}