PistonDevelopers / PistonDevelopers/piston
Memory allocation error when using Events::new(EventSettings::new())
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 4.7k
- Forks
- 235
- Avg merge
- 1m
- Merged PRs (30d)
- 4
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reduced example and compare Events::next(&mut window) with window.next(), confirming the unbounded memory growth. Trace the event-loop entry points used by Events::new(EventSettings::new()) and identify where allocations accumulate. Done means the Events path no longer exhausts memory and the reduced case remains functional.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100