【发布时间】: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!时,它可以工作。我同意:似乎锈蚀破坏时间太早了