This is a mirror of official site: http://jasper-net.blogspot.com/

SetUnhandledExceptionFilter and the C/C++ Runtime Library

| Tuesday, February 8, 2011
Introduction

Windows provides a way for applications to override the default application "crash" handling functionality by the means of SetUnhandledExceptionFilter function.

Usually SetUndhandledExceptionFilter function is used in conjunction with crash reporting activity. Having the ability of pinpointing the line of code which caused a crash is invaluable in post mortem debugging.

Post mortem debugging has been discussed in other articles on CodeProject and is not the scope of this article.

Here is a simple unhandled exception filter (which displays only "Gotcha!" in console) looks like:

bool g_showCrashDialog = false;

LONG WINAPI OurCrashHandler(EXCEPTION_POINTERS * /*ExceptionInfo*/)
{
   std::cout << "Gotcha!" << std::endl;

   return g_showCrashDialog ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER;
}

If the crash handling function returns EXCEPTION_EXECUTE_HANDLER the operating system will display the default crash dialog or call the Just in time (JIT) Debugger if such a debugger is installed on the system.

In order to test the code we will simulate a null pointer invalid access like this:

int main()
{
   ::SetUnhandledExceptionFilter(OurCrashHandler);

   std::cout << "Normal null pointer crash" << std::endl;

   char *p = 0;
   *p = 5;
}

The program should then display:

Normal null pointer crash
Gotcha!
The C/C++ Runtime Library

The C/C++ Runtime Library will remove any custom crash handler in certain circumstances and our crash handler will never be called.

Circumstances such as:

abort() function

void AbortCrash()
{
   std::cout << "Calling Abort" << std::endl;
   abort();
}

Read more: Codeproject

Posted via email from Jasper-net

0 comments: