gpt4 book ai didi

rust - Piston 应用程序在几分钟后因内存分配错误而崩溃

转载 作者:行者123 更新时间:2023-11-29 08:10:28 28 4
gpt4 key购买 nike

为了自学图形/UI 编程,我使用 Rust 中的 Piston 开发了一个迷宫游戏。游戏大部分运行良好,但当我在大型迷宫(例如 120 x 72 矩形)中运行时,游戏会在几分钟后因内存分配错误而崩溃。

一个简化的例子如下:

extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
extern crate piston_window;

use opengl_graphics::OpenGL;
use piston::event_loop::*;
use piston::input::*;
use piston_window::{PistonWindow, WindowSettings};

const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];

fn main() {
let opengl = OpenGL::V3_2;

let mut window: PistonWindow = WindowSettings::new("maze", [800, 600])
.opengl(opengl)
.exit_on_esc(true)
.build()
.unwrap();

let mut events = Events::new(EventSettings::new());

while let Some(event) = events.next(&mut window) {
if let Some(_args) = event.render_args() {
window.draw_2d(&event, |c, gl| {
graphics::clear(BLACK, gl);

for _row in 0..72 {
for _col in 0..120 {
let color = WHITE;
let box_rect =
graphics::rectangle::rectangle_by_corners(0.0, 0.0, 10.0, 10.0);
graphics::rectangle(color, box_rect, c.transform, gl);
}
}
});
}
}
}

当我运行它时,出现内存分配错误,然后进程中止:

$ time cargo run --release
Compiling maze v0.1.0 (/home/isaac/prog/rust/test-maze)
Finished release [optimized] target(s) in 5.28s
Running `target/release/maze`
memory allocation of 7927496704 bytes failedAborted (core dumped)

real 2m24.317s
user 1m29.515s
sys 0m3.644s

在调试器中运行它,我得到以下回溯:

(gdb) backtrace
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff71e1801 in __GI_abort () at abort.c:79
#2 0x0000555555708fc7 in std::sys::unix::abort_internal () at src/libstd/sys/unix/mod.rs:157
#3 0x000055555570419d in rust_oom () at src/libstd/alloc.rs:211
#4 0x0000555555717f37 in alloc::alloc::handle_alloc_error () at src/liballoc/alloc.rs:224
#5 0x00005555556eaf7d in <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::spec_extend ()
#6 0x00005555556e64aa in <gfx_core::handle::Manager<R>>::extend ()
#7 0x00005555556da204 in <gfx_device_gl::Device as gfx_core::Device>::pin_submitted_resources ()
#8 0x000055555559d654 in <gfx::encoder::Encoder<R, C>>::flush ()
#9 0x000055555558f8af in maze::main ()
#10 0x000055555558bb73 in std::rt::lang_start::{{closure}} ()
#11 0x0000555555704913 in std::rt::lang_start_internal::{{closure}} () at src/libstd/rt.rs:49
#12 std::panicking::try::do_call () at src/libstd/panicking.rs:297
#13 0x000055555570927a in __rust_maybe_catch_panic () at src/libpanic_unwind/lib.rs:92
#14 0x0000555555705466 in std::panicking::try () at src/libstd/panicking.rs:276
#15 std::panic::catch_unwind () at src/libstd/panic.rs:388
#16 std::rt::lang_start_internal () at src/libstd/rt.rs:48
#17 0x000055555558fae2 in main ()

我正在使用 Ubuntu Linux 18.04。

我的程序是否应该以不同的方式编写以防止出现此问题? Piston 有问题吗?

最佳答案

问题出在这里:

let mut events = Events::new(EventSettings::new());

while let Some(event) = events.next(&mut window) {

相反,应该使用这个:

while let Some(event) = window.next() {

事实证明,window.next() 运行了一些 events.next(&mut window) 省略的清理步骤,这些清理步骤释放了相关内存。据我所知,文档没有提及这一点,示例使用了两种模式,但没有指出区别。

此清理被提及 in the comments here ,正如 Tiriosaurus on reddit 指出的那样.

关于rust - Piston 应用程序在几分钟后因内存分配错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000664/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com