Nathan Osman
Nathan Osman

Reputation: 73295

How to handle seg faults under Windows?

How can a Windows application handle segmentation faults? By 'handle' I mean intercept them and perhaps output a descriptive message. Also, the ability to recover from them would be nice too, but I assume that is too complicated.

Upvotes: 7

Views: 10547

Answers (6)

Ana Betts
Ana Betts

Reputation: 74702

Let them crash and let the Windows Error Reporting handle it - under Vista+, you should also consider registering with Restart Manager (http://msdn.microsoft.com/en-us/library/aa373347(VS.85).aspx), so that you have a chance to save out the user's work and restart the application (like what Word/Excel/etc.. does)

Upvotes: 5

shoosh
shoosh

Reputation: 79021

If you add the /EHa compiler argument then try {} catch(...) will catch all exceptions for you, including SEH exceptions.
You can also use __try {} __except {} which gives you more flexibility on what to do when an exception is caught. putting an __try {} __except {} on your entire main() function is somewhat equivalent to using SetUnhandeledExceptionFilter().

That being said, you should also use the proper terminology: "seg-fault" is a UNIX term. There are no segmentation faults on Windows. On Windows they are called "Access Violation Exceptions"

Upvotes: 3

gbburkhardt
gbburkhardt

Reputation: 11

Similar to Jean-François Fabre solution, but with Posix code in MinGW-w64. But note that the program must exit - it can't recover from the SIGSEGV and continue.

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void sigHandler(int s)
{
    printf("signal %d\n", s);
    exit(1);
}

int main()
{
    signal(SIGSEGV, sigHandler);
    int *v=0;
    *v = 0;   // trigger the fault

    return 0; 
}

Upvotes: 0

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140307

C++ self-contained example on how to use SetUnhandledExceptionFilter, triggering a write fault and displaying a nice error message:

#include <windows.h>
#include <sstream>

LONG WINAPI TopLevelExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
  std::stringstream s;
  s << "Fatal: Unhandled exception 0x" << std::hex << pExceptionInfo->ExceptionRecord->ExceptionCode 
  << std::endl;

  MessageBoxA(NULL, s.str().c_str(), "my application", MB_OK | MB_ICONSTOP);
  exit(1);

  return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
   SetUnhandledExceptionFilter(TopLevelExceptionHandler);
   int *v=0;
   v[12] = 0;   // should trigger the fault
   return 0;
}

Tested successfully with g++ (and should work OK with MSVC++ as well)

Upvotes: 1

bmargulies
bmargulies

Reputation: 100196

What you want to do here depends on what sort of faults you are concerned with. If you have sloppy code that is prone to more or less random General Protection Violations, then @Paul Betts answer is what you need.

If you have code that has a good reason to deference bad pointers, and you want to recover, start from @whunmr's suggestion about SEH. You can handle and indeed recover, if you have clear enough control of your code to know exactly what state it is in at the point of the fault and how to go about recovering.

Upvotes: 0

whunmr
whunmr

Reputation: 2435

Use SEH for early exception handling, and use SetUnhandledExceptionFilter to show a descriptive message.

Upvotes: 3

Related Questions