0

TIL that my notions of the 'inter-twining' (for the lack of a better word) of RAII & stack-unwinding are/were quite(if not completely) wrong. My understanding was that using RAII, guarded against any/all resource leaks - even ones potentially caused by unhandled exceptions.

However writing this test program and subsequently stumbling upon this article/documentation, made me realize that stack unwinding would only cause the RAII-enabled resource deallocation to kick in for automatic's within the try block as opposed to automatic's in, say, outer/other scopes.

Am I correct in this (new) understanding? Or are there further nuances I am yet not grasping? Any gurus out there want to chime in? Pointers to any good write-ups/analyses/explanations (of stack-unwinding) would be helpful/appreciated…

1

3 Answers 3

5

From the C++03 standard, §15.3/9:

If no matching handler is found in a program, the function terminate() is called; whether or not the stack is unwound before this call to terminate() is implementation-defined (15.5.1).

§15.5.1/1:

In the following situations exception handling must be abandoned for less subtle error handling techniques: ... when the exception handling mechanism cannot find a handler for a thrown exception (15.3) ...

§15.5.1/2:

In such cases,

    void terminate();

is called (18.6.3). In the situation where no matching handler is found, it is implementation-defined whether or not the stack is unwound before terminate() is called. In all other situations, the stack shall not be unwound before terminate() is called. An implementation is not permitted to finish stack unwinding prematurely based on a determination that the unwind process will eventually cause a call to terminate().

3
  • Is this document online? If so, could you add in a link to the one you are quoting from? Thanks. (My search skills aren't up to snuff and I don't seem to be able to find it.) Commented Apr 5, 2011 at 20:08
  • @decimus phostle : It's an ISO standard, so no, not for free, but you can buy it here: iso.org/iso/catalogue_detail.htm?csnumber=38110. Alternatively, you can read the C++0x drafts for free, the latest of which is here: open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf. However, keep in mind that many, many changes are in C++0x so much of what's in there only applies to bleeding edge compilers.
    – ildjarn
    Commented Apr 5, 2011 at 20:21
  • @decimus: the full title of the current standard is ISO/IEC 14882:2003(E). Google it. Or as ildjarn says, look forward to the next version. It'll be ratified any month now, but nobody fully implements it yet. Commented Apr 5, 2011 at 23:23
2

You are right that "stack unwinding" happens on the way from the throw some_exception to catch(some_exception). If your exception never reaches a catch, we don`t known what happens.

Is that a big problem? As you have shown yourself, you just have to add a catch(...) somewhere to catch all possible exceptions, and the problem goes away.

3
  • What happens if an exception is not caught is not unknown. Just read ildjarn's responce.
    – Tobias
    Commented Apr 5, 2011 at 21:10
  • The program will terminate of course, but what is unknown is whether any "stack unwinding" happens first. That is easy to fix by adding a catch(...).
    – Bo Persson
    Commented Apr 5, 2011 at 21:43
  • More accurate than "unknown": When a thrown exception has no matching catch, it is implementation-defined whether or not the stack unwinds before terminate is called.
    – aschepler
    Commented Apr 6, 2011 at 16:15
0

The Standard defines three ways to end execution of a C++ program:

  • Return from main. Objects with automatic storage (function-local) have already been destroyed. Objects with static storage (global, class-static, function-static) will be destroyed.
  • std::exit from <cstdlib>. Objects with automatic storage are NOT destroyed. Objects with static storage will be destroyed.
  • std::abort from <cstdlib>. Objects with automatic and static storage are NOT destroyed.

Also relevant is std::terminate from <exception>. The behavior of terminate can be replaced using std::set_terminate, but terminate must always "terminate execution of the program" by calling abort or some similar implementation-specific alternative. The default is just { std::abort(); }.

C++ will call std::terminate whenever an exception is thrown and C++ can't reasonably do stack unwinding. For example, an exception from a destructor called by stack unwinding or an exception from a static storage object constructor or destructor. In these cases, there is no (more) stack unwinding done.

C++ will also call std::terminate when a matching catch handler is not found. In this single case, C++ may optionally unwind to main before calling terminate. So your example might have different results with a different compiler.

So if you use RAII correctly, the remaining steps to "leak-proof" your program are:

  • Avoid std::abort.
  • Either avoid std::exit or avoid all objects with static storage duration.
  • Put a catch (...) handler in main, and make sure no allocations or exceptions happen in or after it.
  • Avoid the other programming errors that can cause std::terminate.
    • (On some implementations, functions compiled with a C compiler act like they have C++'s empty throw() specification, meaning that exceptions cannot be thrown "past" them even though they have no destructors to be called.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.