【问题标题】:Why does an atexit handler panic when it accesses stdout?为什么 atexit 处理程序在访问标准输出时会出现恐慌?
【发布时间】:2016-03-14 05:29:14
【问题描述】:

下面的 Rust 程序在访问 atexit 处理程序中的 stdout 时发生恐慌。

extern crate libc;

extern "C" fn bye() {
    println!("bye");
}

fn main() {
    println!("hello");
    unsafe { libc::atexit(bye) };
}

输出:

hello
thread '<main>' panicked at 'cannot access stdout during shutdown', ../src/libcore/option.rs:298
fatal runtime error: Could not unwind stack, error = 5
An unknown error occurred

在我看来this registration 应该在我们的atexit 注册之前运行,所以处理程序中的this line 应该只在我们的自定义处理程序之后运行。因此它不应该恐慌。

【问题讨论】:

  • 我遇到了同样的问题,虽然我的错误代码是pointer being freed was not allocated。当用libc::printf 替换println! 时,它可以工作。我同意:似乎锈蚀破坏时间太早了

标签: rust atexit


【解决方案1】:

您混淆了您调用的 libc::atexit 和您的链接指向的 sys_common::at_exit(在 src/libstd/sys/common/mod.rs 中)以及 Rust 在早期清理期间调用的 sys_common::at_exit

这是两个不同的清理队列,我不想依赖它们按特定顺序执行。

【讨论】:

    猜你喜欢
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多