在调试模式下运行包含过多 include_bytes!(...) 的 Rust 代码时出现堆栈溢出

问题描述 投票:0回答:1

我有一个中等大小的 Rust 项目,它可以成功编译且无警告,并使用

cargo run --release
成功运行。但是,
cargo run
失败,并显示
error: process didn't exit successfully: `target\\debug\\blah.exe` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)

我能够将问题跟踪到我对八个不同文件执行

include_bytes!(...)
的地方;其中一些比其他的大(大约 340 Kb;其余的大约为 130、90、75、50、45 和 10)。删除两个较大文件中任何一个的负载(并且仅针对这两个文件)可以阻止
cargo run
崩溃。以防万一我尝试使用
static
而不是本地
let
- 具有相同的结果,
cargo run
因堆栈溢出而崩溃,
cargo run --release
则不会。 stable-x86_64-pc-windows-msvc 和 nightly-x86_64-pc-windows-msvc 都有相同的结果。默认为稳定,
rustc --version
报告 1.73.0。我希望能够在不崩溃的情况下完成
cargo run

下面的代码说明了这个问题:

fn main() {
    let tmp1 = *include_bytes!("test.wav");
    let tmp2 = *include_bytes!("test.wav");
    let tmp3 = *include_bytes!("test.wav");
}

其中

test.wav
是一个适当大的文件,位于
main.rs
旁边。

rust stack-overflow
1个回答
0
投票

如果您要调用的方法需要

Box<[T]>
,您应该直接将
include_bytes!
生成的数组引用转换为装箱切片:

let tmp1 = Box::from(include_bytes!("test.wav"));

这样就可以避免在堆栈上分配不必要的大数组。

© www.soinside.com 2019 - 2024. All rights reserved.