std :: terminate()触发堆栈放松吗?

发布于 2025-01-18 18:48:15 字数 522 浏览 2 评论 0原文

我一直在尝试实现 Exception 类,对于程序终止,我决定使用 std::terminate() ,但无论是否使用,我都不会使用std::terminate() 触发堆栈展开过程。

例如,如果我编译并运行此代码:

struct Test {
    Test() {
        std::cout << "Constructed\n";
    }
    ~Test() {
        std::cout << "Destructed\n";
    }
};

int main() {
    Test t;
    std::terminate();
    return 0;
}

它将输出:

Constructed
terminate called without an active exception

并且似乎析构函数没有被调用。

I've been trying to implement Exception class, and for program termination i've decided to use std::terminate(), but i'm not suse whether or not std::terminate() triggers stack unwinding process.

For example, if i compile and run this code:

struct Test {
    Test() {
        std::cout << "Constructed\n";
    }
    ~Test() {
        std::cout << "Destructed\n";
    }
};

int main() {
    Test t;
    std::terminate();
    return 0;
}

it will output this:

Constructed
terminate called without an active exception

and it seems that destructor is not being called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-01-25 18:48:15

std::terminate() 的标准处理程序直接调用 std::abort

如果您查看此处,您会发现std::abort() 没有调用任何析构函数。

不调用具有自动、线程局部(C++11 起)和静态存储持续时间的变量的析构函数。使用 std::atexit() 和 std::at_quick_exit (C++11 起) 注册的函数也不会被调用。是否关闭打开的资源(例如文件)是实现定义的。实现定义的状态将返回到主机环境,指示执行不成功。

The standard handler for std::terminate() calls directly std::abort.

If you take a look here, you will find out that std::abort() did not call any of the destructors.

Destructors of variables with automatic, thread local (since C++11) and static storage durations are not called. Functions registered with std::atexit() and std::at_quick_exit (since C++11) are also not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文