C ++ abort override

Some C ++ libraries call the abort () function in case of an error (e.g. SDL). In this case, useful debugging information is not provided. Unable to catch the interrupt call and write any diagnostic log output. I would like to redefine this behavior throughout the world without rewriting or rebuilding these libraries. I would like to throw an exception and handle it. Is it possible?

+5
source share
3 answers

Notice what aborttriggers the signal SIGABRTas if it were called raise(SIGABRT). You can install a signal handler that is called in this situation, for example:

#include <signal.h>

extern "C" void my_function_to_handle_aborts(int signal_number)
{
    /*Your code goes here. You can output debugging info.
      If you return from this function, and it was called 
      because abort() was called, your program will exit or crash anyway
      (with a dialog box on Windows).
     */
}

/*Do this early in your program initialization */
signal(SIGABRT, &my_function_to_handle_aborts);

abort (, - , , ), . ANSI C, Unix Windows, , , , . , , assert - , malloc . , . - , . , , .

abort. , Windows Visual ++ _set_abort_behavior, , , .

+17

man Linux, abort() SIGABRT , . EDIT: , Windows - . .

+3

, std:: abort. , .

0

All Articles