Chapter 6. Termination

Table of Contents

Termination Handlers
Verbose Terminate Handler

Termination Handlers

Not many changes here to cstdlib. You should note that the abort() function does not call the destructors of automatic nor static objects, so if you're depending on those to do cleanup, it isn't going to happen. (The functions registered with atexit() don't get called either, so you can forget about that possibility, too.)

The good old exit() function can be a bit funky, too, until you look closer. Basically, three points to remember are:

  1. Static objects are destroyed in reverse order of their creation.

  2. Functions registered with atexit() are called in reverse order of registration, once per registration call. (This isn't actually new.)

  3. The previous two actions are “interleaved,” that is, given this pseudocode:

      extern "C or C++" void  f1 (void);
      extern "C or C++" void  f2 (void);
      
      static Thing obj1;
      atexit(f1);
      static Thing obj2;
      atexit(f2);
    

    then at a call of exit(), f2 will be called, then obj2 will be destroyed, then f1 will be called, and finally obj1 will be destroyed. If f1 or f2 allow an exception to propagate out of them, Bad Things happen.

Note also that atexit() is only required to store 32 functions, and the compiler/library might already be using some of those slots. If you think you may run out, we recommend using the xatexit/xexit combination from libiberty, which has no such limit.