piston icon indicating copy to clipboard operation
piston copied to clipboard

Memory allocation error when using Events::new(EventSettings::new())

Open isaacg1 opened this issue 5 years ago • 0 comments

When using an event loop created with Events::new(EventSettings::new()) , more and more meory is used up until the process aborts with an out of memory error. The problem can be avoided by switching to use window.next() instead.

Here's a reduced test case:

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

The program aborts in about 2 minutes after attempting to allocate 8GB. If events is removed and window.next() is used in the while loop instead, the problem goes away.

isaacg1 avatar Jan 02 '19 17:01 isaacg1