栈展开:在程序发生异常(或函数提前退出)时,运行时系统按调用栈的逆序“退栈”,依次销毁局部对象、执行清理代码(如 C++ 析构函数、finally 块),并把控制权传递到匹配的异常处理位置。
/stæk ʌnˈwaɪndɪŋ/
Stack unwinding frees local resources when an exception is thrown.
当抛出异常时,栈展开会释放局部资源。
During stack unwinding, each destructor runs in reverse order of construction, ensuring the file handle is closed even if the function fails.
在栈展开过程中,每个析构函数会按构造的相反顺序执行,从而即使函数失败也能确保文件句柄被关闭。
stack 原指“堆/叠起来的一摞”,在计算机里引申为“调用栈”(像盘子一样后进先出叠放);unwinding 来自 unwind(“解开、松开缠绕”)。合在一起比喻:当异常发生时,把“卷起来”的函数调用一层层“解开”,回退到更外层的调用点,并完成必要的清理。