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="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. 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 <code class="filename">cstdlib</code>. You should note that the 7 <code class="function">abort()</code> function does not call the 8 destructors of automatic nor static objects, so if you're 9 depending on those to do cleanup, it isn't going to happen. 10 (The functions registered with <code class="function">atexit()</code> 11 don't get called either, so you can forget about that 12 possibility, too.) 13 </p><p> 14 The good old <code class="function">exit()</code> function can be a bit 15 funky, too, until you look closer. Basically, three points to 16 remember are: 17 </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p> 18 Static objects are destroyed in reverse order of their creation. 19 </p></li><li class="listitem"><p> 20 Functions registered with <code class="function">atexit()</code> are called in 21 reverse order of registration, once per registration call. 22 (This isn't actually new.) 23 </p></li><li class="listitem"><p> 24 The previous two actions are <span class="quote">“<span class="quote">interleaved,</span>”</span> that is, 25 given this pseudocode: 26 </p><pre class="programlisting"> 27 extern "C or C++" void f1 (void); 28 extern "C or C++" void f2 (void); 29 30 static Thing obj1; 31 atexit(f1); 32 static Thing obj2; 33 atexit(f2); 34</pre><p> 35 then at a call of <code class="function">exit()</code>, 36 <code class="varname">f2</code> will be called, then 37 <code class="varname">obj2</code> will be destroyed, then 38 <code class="varname">f1</code> will be called, and finally 39 <code class="varname">obj1</code> will be destroyed. If 40 <code class="varname">f1</code> or <code class="varname">f2</code> allow an 41 exception to propagate out of them, Bad Things happen. 42 </p></li></ol></div><p> 43 Note also that <code class="function">atexit()</code> is only required to store 32 44 functions, and the compiler/library might already be using some of 45 those slots. If you think you may run out, we recommend using 46 the <code class="function">xatexit</code>/<code class="function">xexit</code> combination from <code class="literal">libiberty</code>, which has no such limit. 47 </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> 48 If you are having difficulty with uncaught exceptions and want a 49 little bit of help debugging the causes of the core dumps, you can 50 make use of a GNU extension, the verbose terminate handler. 51 </p><pre class="programlisting"> 52#include <exception> 53 54int main() 55{ 56 std::set_terminate(__gnu_cxx::__verbose_terminate_handler); 57 ... 58 59 throw <em class="replaceable"><code>anything</code></em>; 60} 61</pre><p> 62 The <code class="function">__verbose_terminate_handler</code> function 63 obtains the name of the current exception, attempts to demangle 64 it, and prints it to stderr. If the exception is derived from 65 <code class="classname">exception</code> then the output from 66 <code class="function">what()</code> will be included. 67 </p><p> 68 Any replacement termination function is required to kill the 69 program without returning; this one calls abort. 70 </p><p> 71 For example: 72 </p><pre class="programlisting"> 73#include <exception> 74#include <stdexcept> 75 76struct argument_error : public std::runtime_error 77{ 78 argument_error(const std::string& s): std::runtime_error(s) { } 79}; 80 81int main(int argc) 82{ 83 std::set_terminate(__gnu_cxx::__verbose_terminate_handler); 84 if (argc > 5) 85 throw argument_error("argc is greater than 5!"); 86 else 87 throw argc; 88} 89</pre><p> 90 With the verbose terminate handler active, this gives: 91 </p><pre class="screen"> 92 <code class="computeroutput"> 93 % ./a.out 94 terminate called after throwing a `int' 95 Aborted 96 % ./a.out f f f f f f f f f f f 97 terminate called after throwing an instance of `argument_error' 98 what(): argc is greater than 5! 99 Aborted 100 </code> 101 </pre><p> 102 The 'Aborted' line comes from the call to 103 <code class="function">abort()</code>, of course. 104 </p><p> 105 This is the default termination handler; nothing need be done to 106 use it. To go back to the previous <span class="quote">“<span class="quote">silent death</span>”</span> 107 method, simply include <code class="filename">exception</code> and 108 <code class="filename">cstdlib</code>, and call 109 </p><pre class="programlisting"> 110 std::set_terminate(std::abort); 111 </pre><p> 112 After this, all calls to <code class="function">terminate</code> will use 113 <code class="function">abort</code> as the terminate handler. 114 </p><p> 115 Note: the verbose terminate handler will attempt to write to 116 stderr. If your application closes stderr or redirects it to an 117 inappropriate location, 118 <code class="function">__verbose_terminate_handler</code> will behave in 119 an unspecified manner. 120 </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"> Chapter 5. 121 Diagnostics 122 123</td></tr></table></div></body></html>