几分钟后,活塞应用程序由于内存分配错误而崩溃

时间:2019-01-02 02:46:17

标签: rust out-of-memory rust-piston

我在Rust中使用Piston开发了一个迷宫游戏,以自学图形/ UI编程。该游戏通常可以正常运行,但是当我使用大型迷宫(例如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。

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

2 个答案:

答案 0 :(得分:5)

问题位于此处:

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的清理工作。

答案 1 :(得分:2)

这大约是8 GB的分配空间;太大而无法理解。 8 GB可能与您的系统RAM有关。等于其一半或两倍,具体取决于您的系统设置。

问题必须在循环中并且必须很小。循环不是很大,是吗?关于小例子,这是一件好事。看起来好像是事件代码。我不知道为什么。

这是一个似乎没有内存泄漏的版本:

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();

    while let Some(event) = window.next() {
        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);
                }
            }
        });
    }
}