3

I'm trying to catch a signal so I can make our program exit gracefully. SIGABRT happens when we read a bad file, which we can't control. This is a multi-platform program so we need something that works in windows, linux, and mac.

For some reason, when I add a signal_callback_handler like at signal handling example, and run the problem test in Windows, it's still coming up with the abort popup box like we had before. How do I redirect before the popup for abort happens? I want our program to exit gracefully.

//constructor
example::example(const string theString) 
{
   signal(SIGABRT, signal_callback_handler);
}

void example::signal_callback_handler(int sigNum)
{
   //want to handle gracefully here, but it's not getting caught
}

bool example::someMethod()
{
   FileHandle.openFile(problemDocument); //this openFile is where the SIGABRT happens
}

Thanks!

Michele
  • 3,617
  • 12
  • 47
  • 81
  • Is this [answer](http://stackoverflow.com/questions/8934879/how-to-handle-sigabrt-signal) helpful for your question ? – HDJEMAI Oct 22 '15 at 19:40
  • I saw that example, and Antoine Matthys' example is very similar to mine. I was looking at the example below it and had added a comment. I still get the abort popup. I want to exit the code my own way. I realize the code can't continue as-is, but it's not hitting my breakpoint at all that I added in the handler. – Michele Oct 22 '15 at 19:45
  • 1
    Are you really allowed to pass a class method to `signal`? I'd think that wouldn't work on many systems, since `example::signal_callback_handler()` is expecting to get passed its invisible `this` parameter, and the system signal handling code has no way of doing that. So depending on calling conventions, it may get a totally messed up set of arguments. What happens if you use a callback function which is not a method? – Nate Eldredge Oct 23 '15 at 02:01
  • 1
    @Nate: presumably it's a static member function, or the compiler would have a fit. Technically illegal per the C++ standard, IMO, because of the different language linkage, but in practice it should work (at least on Windows). – Harry Johnston Oct 23 '15 at 02:47
  • I did make it static. Thanks for the info. – Michele Oct 26 '15 at 13:58

1 Answers1

1

The behaviour you describe is as documented.

From the Visual Studio documentation for abort (emphasis mine):

By default, when an app is built with the debug runtime library, the abort routine displays an error message before SIGABRT is raised. [...] To suppress the message, use _set_abort_behavior to clear the _WRITE_ABORT_MSG flag.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Thanks. I realized that after I accept the abort box it actually hits my breakpoint in my signal handler method. I think my team member is going to over-ride abort and we'll handle it as a regular exception instead of handling SIGABRT. – Michele Oct 26 '15 at 13:59
  • 1
    You could just use _set_abort_behavior if you preferred. And do note that this only affects debug builds. – Harry Johnston Oct 26 '15 at 22:24
  • We need this for both debug and production – Michele Oct 29 '15 at 12:18