![]() |
VOOZH | about |
Stack unwinding is the process of removing function call frames from the call stack at runtime. During this process, local objects are destroyed in the reverse order of their creation.
Stack unwinding happens:
When an exception is not handled in the current function, the runtime:
Example: Stack Unwinding with Exception Handling
f3() Start f2() Start f1() Start Caught Exception: 100 f3() End
Explanation:
Note that the following lines inside f1() and f2() are not executed at all.
cout<<"f1() End \n"; // inside f1()
cout<<"f2() End \n"; // inside f2()
If there were some local class objects inside f1() and f2(), destructors for those local objects would have been called in the Stack Unwinding process.
Stack unwinding does not automatically free heap memory allocated using new unless it is managed by an object whose destructor handles cleanup.
Example:
Resource allocated: 10 An error occurred
Explanation: