Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBox "Abnormal program termination" keeps my application running

...kind of. As illustrated by this extremely simplistic example,

enter image description here

very rarely (only once reported so far), it happens that one of my applications crashes this way. I want to terminate it as I normally do when an unspecific exception occurs. My strategy is to (low-level) log the problem, then terminate. The application is part of a subsystem and I want to (re)start it, if any problem is detected. It's built with C++-Builder 6 and runs on Windows (XP...7, also 8). I learned that an abort() most probably caused the error message. The application has a GUI, that's why a message box is shown instead of just making an (unblocking) output to stderr.

And as long as the message box isn't accepted by a user, my application keeps obviously running, for example it handles timers (the lifebeats in the above example increase) or inter-process messages, fully unaware about the problem.

After reading some answers to What is the easiest way to make a C++ program crash? and Difference between raise(SIGABRT) and abort() methods, I tried the following

void mySignalHandler(int sig)
{
    // low-level error reporting here
    exit(-1);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    signal(SIGABRT, mySignalHandler);
    // some more initialisation here
}

which lets my application terminate properly also if abort() or raise(SIGABRT) is called. (I also wish to prevent Windows from "searching for a solution of the problem".)

Is this (registering a signal handler for abort and calling exit there) reliable from your point of view? ...or at least something one can build upon?

like image 710
Wolf Avatar asked Aug 14 '14 10:08

Wolf


People also ask

What is abnormal program termination in C?

Abnormal program termination (C++) This is a run-time error. This error is generated when the program calls abort.

Which error will result the abnormal termination of program?

An error that causes abnormal termination of program during running time is Runtime Error.


1 Answers

In the C++Builder installation folder, check the following files:

  • source\cpprtl\Source\misc\errormsg.c - implementation of _ErrorMessage
  • source\cpprtl\Source\procses\abort.c - implementation of abort, which calls _ErrorMessage
  • source\cpprtl\Source\misc\assert.c - implementation of _assert, which calls _ErrorMessage

errormsg.c defines an undocumented _messagefunc function pointer that you can set to override the default behavior. Although it's undocumented and not declared in any header files, you can declare it as an extern and access it that way. Sample usage:

extern int (_RTLENTRY * _EXPDATA _messagefunc)(char *msg);

static int LogAndDie(char *msg)
{
  LogMessageToSomeFile(msg);
  exit(1);
  return 0;
}

void InitializeErrorHandling()
{
  _messagefunc = LogAndDie;
}
like image 147
Josh Kelley Avatar answered Oct 18 '22 18:10

Josh Kelley