What is stack unwinding? Searched through but couldn't find enlightening answer! Thanks in advance.
I don't know if you read this yet, but Wikipedia's article on the call stack has a decent explanation.
Stack unwinding is a mostly C++ concept, dealing with how stack-allocated objects are destroyed when its scope is exited (either normally, or through an exception).
Say you have this fragment of code:
void hw() {
string hello("Hello, ");
string world("world!\n");
cout << hello << world;
} // at this point, "world" is destroyed, followed by "hello"
In a general sense, a stack "unwind" is pretty much synonymous with the end of a function call and the subsequent popping of the stack.
However, specifically in the case of C++, stack unwinding has to do with how C++ calls the destructors for the objects allocated since the started of any code block. Objects that were created within the block are deallocated in reverse order of their allocation.
All this relates to C++:
Definition: As you create objects statically (on the stack as opposed to allocating them in the heap memory) and perform function calls, they are "stacked up".
When a scope (anything delimited by {
and }
) is exited (by using return XXX;
, reaching the end of the scope or throwing an exception) everything within that scope is destroyed (destructors are called for everything). This process of destroying local objects and calling destructors is called stack unwinding. (Exiting a code block using goto
will not unwind the stack which is one of the reasons you should never use goto
in C++).
You have the following issues related to stack unwinding:
avoiding memory leaks (anything dynamically allocated that is not managed by a local object and cleaned up in the destructor will be leaked) - see RAII referred to by Nikolai, and the documentation for boost::scoped_ptr or this example of using boost::mutex::scoped_lock.
program consistency: the C++ specifications state that you should never throw an exception before any existing exception has been handled. This means that the stack unwinding process should never throw an exception (either use only code guaranteed not to throw in destructors, or surround everything in destructors with
try {
and} catch(...) {}
).
If any destructor throws an exception during stack unwinding you end up in the land of undefined behavior which could cause your program to treminate unexpectedly (most common behavior) or the universe to end (theoretically possible but has not been observed in practice yet).
When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding. The automatic objects are destroyed in reverse order of their construction. (Automatic objects are local objects that have been declared auto or register, or not declared static or extern. An automatic object x is deleted whenever the program exits the block in which x is declared.)
If an exception is thrown during construction of an object consisting of subobjects or array elements, destructors are only called for those subobjects or array elements successfully constructed before the exception was thrown. A destructor for a local static object will only be called if the object was successfully constructed.