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">
3 <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 V1.75.2" /><meta name=
"keywords" content=
" ISO C++ , library " /><link rel=
"home" href=
"../spine.html" title=
"The GNU C++ Library Documentation" /><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 width=
"20%" align=
"left"><a accesskey=
"p" href=
"dynamic_memory.html">Prev
</a> </td><th width=
"60%" align=
"center">Chapter
4.
6 </th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"diagnostics.html">Next
</a></td></tr></table><hr /></div><div class=
"sect1" title=
"Termination"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a id=
"std.support.termination"></a>Termination
</h2></div></div></div><div class=
"sect2" title=
"Termination Handlers"><div class=
"titlepage"><div><div><h3 class=
"title"><a id=
"support.termination.handlers"></a>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
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
18 </p><div class=
"orderedlist"><ol class=
"orderedlist" type=
"1"><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);
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=
"sect2" title=
"Verbose Terminate Handler"><div class=
"titlepage"><div><div><h3 class=
"title"><a id=
"support.termination.verbose"></a>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
<exception
>
57 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
60 throw
<em class=
"replaceable"><code>anything
</code></em>;
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.
69 Any replacement termination function is required to kill the
70 program without returning; this one calls abort.
73 </p><pre class=
"programlisting">
74 #include
<exception
>
75 #include
<stdexcept
>
77 struct argument_error : public std::runtime_error
79 argument_error(const std::string
& s): std::runtime_error(s) { }
84 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
86 throw argument_error(
<span class=
"quote">“
<span class=
"quote">argc is greater than
5!
</span>”
</span>);
91 With the verbose terminate handler active, this gives:
92 </p><pre class=
"screen">
93 <code class=
"computeroutput">
95 terminate called after throwing a `int'
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!
103 The 'Aborted' line comes from the call to
104 <code class=
"function">abort()
</code>, of course.
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);
113 After this, all calls to
<code class=
"function">terminate
</code> will use
114 <code class=
"function">abort
</code> as the terminate handler.
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 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=
"../spine.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Chapter
5.
124 </td></tr></table></div></body></html>