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