Home | History | Annotate | Line # | Download | only in manual
      1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Termination</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="support.html" title="Chapter4. Support" /><link rel="prev" href="dynamic_memory.html" title="Dynamic Memory" /><link rel="next" href="diagnostics.html" title="Chapter5. Diagnostics" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Termination</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="dynamic_memory.html">Prev</a></td><th width="60%" align="center">Chapter4.
      3   Support
      4   
      5 </th><td width="20%" align="right"><a accesskey="n" href="diagnostics.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.support.termination"></a>Termination</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="support.termination.handlers"></a>Termination Handlers</h3></div></div></div><p>
      6       Not many changes here to
      7       <code class="filename">&lt;cstdlib&gt;</code>.
      8       You should note that the
      9       <code class="function">abort()</code> function does not call the
     10       destructors of automatic nor static objects, so if you're
     11       depending on those to do cleanup, it isn't going to happen.
     12       (The functions registered with <code class="function">atexit()</code>
     13       don't get called either, so you can forget about that
     14       possibility, too.)
     15     </p><p>
     16       The good old <code class="function">exit()</code> function can be a bit
     17       funky, too, until you look closer.  Basically, three points to
     18       remember are:
     19     </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
     20 	Static objects are destroyed in reverse order of their creation.
     21 	</p></li><li class="listitem"><p>
     22 	Functions registered with <code class="function">atexit()</code> are called in
     23 	reverse order of registration, once per registration call.
     24 	(This isn't actually new.)
     25 	</p></li><li class="listitem"><p>
     26 	The previous two actions are <span class="quote"><span class="quote">interleaved,</span></span> that is,
     27 	given this pseudocode:
     28 	</p><pre class="programlisting">
     29   extern "C or C++" void f1 ();
     30   extern "C or C++" void f2 ();
     31 
     32   static Thing obj1;
     33   atexit(f1);
     34   static Thing obj2;
     35   atexit(f2);
     36 </pre><p>
     37 	then at a call of <code class="function">exit()</code>,
     38 	<code class="varname">f2</code> will be called, then
     39 	<code class="varname">obj2</code> will be destroyed, then
     40 	<code class="varname">f1</code> will be called, and finally
     41 	<code class="varname">obj1</code> will be destroyed. If
     42 	<code class="varname">f1</code> or <code class="varname">f2</code> allow an
     43 	exception to propagate out of them, Bad Things happen.
     44 	</p></li></ol></div><p>
     45       Note also that <code class="function">atexit()</code> is only required to store 32
     46       functions, and the compiler/library might already be using some of
     47       those slots.  If you think you may run out, we recommend using
     48       the <code class="function">xatexit</code>/<code class="function">xexit</code> combination
     49       from <code class="literal">libiberty</code>, which has no such limit.
     50     </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="support.termination.verbose"></a>Verbose Terminate Handler</h3></div></div></div><p>
     51       If you are having difficulty with uncaught exceptions and want a
     52       little bit of help debugging the causes of the core dumps, you can
     53       make use of a GNU extension, the verbose terminate handler.
     54     </p><p>
     55       The verbose terminate handler is only available for hosted environments
     56       (see <a class="xref" href="configure.html" title="Configure">Configuring</a>) and will be used
     57       by default unless the library is built with
     58       <code class="option">--disable-libstdcxx-verbose</code>
     59       or with exceptions disabled.
     60       If you need to enable it explicitly you can do so by calling the
     61       <code class="function">std::set_terminate</code> function.
     62     </p><pre class="programlisting">
     63 #include &lt;exception&gt;
     64 
     65 int main()
     66 {
     67   std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
     68   ...
     69 
     70   throw <em class="replaceable"><code>anything</code></em>;
     71 }
     72 </pre><p>
     73      The <code class="function">__verbose_terminate_handler</code> function
     74      obtains the name of the current exception, attempts to demangle
     75      it, and prints it to <code class="literal">stderr</code>.
     76      If the exception is derived from
     77      <code class="classname">std::exception</code> then the output from
     78      <code class="function">what()</code> will be included.
     79    </p><p>
     80      Any replacement termination function is required to kill the
     81      program without returning; this one calls <code class="function">std::abort</code>.
     82    </p><p>
     83      For example:
     84    </p><pre class="programlisting">
     85 #include &lt;exception&gt;
     86 #include &lt;stdexcept&gt;
     87 
     88 struct argument_error : public std::runtime_error
     89 {
     90   argument_error(const std::string&amp; s): std::runtime_error(s) { }
     91 };
     92 
     93 int main(int argc)
     94 {
     95   std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
     96   if (argc &gt; 5)
     97     throw argument_error("argc is greater than 5!");
     98   else
     99     throw argc;
    100 }
    101 </pre><p>
    102      With the verbose terminate handler active, this gives:
    103    </p><pre class="screen">
    104    <code class="computeroutput">
    105    % ./a.out
    106    terminate called after throwing a `int'
    107    Aborted
    108    % ./a.out f f f f f f f f f f f
    109    terminate called after throwing an instance of `argument_error'
    110    what(): argc is greater than 5!
    111    Aborted
    112    </code>
    113    </pre><p>
    114      The 'Aborted' line is printed by the shell after the process exits
    115      by calling <code class="function">abort()</code>.
    116    </p><p>
    117      As this is the default termination handler, nothing need be done to
    118      use it.  To go back to the previous <span class="quote"><span class="quote">silent death</span></span>
    119      method, simply include
    120      <code class="filename">&lt;exception&gt;</code> and
    121      <code class="filename">&lt;cstdlib&gt;</code>, and call
    122    </p><pre class="programlisting">
    123      std::set_terminate(std::abort);
    124    </pre><p>
    125      After this, all calls to <code class="function">terminate</code> will use
    126      <code class="function">abort</code> as the terminate handler.
    127    </p><p>
    128      Note: the verbose terminate handler will attempt to write to
    129      <code class="literal">stderr</code>.  If your application closes
    130      <code class="literal">stderr</code> or redirects it to an inappropriate location,
    131      <code class="function">__verbose_terminate_handler</code> will behave in
    132      an unspecified manner.
    133    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dynamic_memory.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="support.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="diagnostics.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Dynamic Memory</td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top">Chapter5.
    134   Diagnostics
    135   
    136 </td></tr></table></div></body></html>