PistonDevelopers / piston

A modular game engine written in Rust

Home Page:https://www.piston.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak with texture.update

amarao opened this issue · comments

(My apology if I report it in the wrong place, user forum proposes to report it as a bug).

I've tried to debug my application, but found that there is a huge memory leak (~30GB in 30-40seconds). When I reduced program to smallest possible, I got this:

extern crate piston_window;
extern crate image as im;

use piston_window::*;
use piston::event_loop::Events;

fn main() {
    let x = 1920;
    let y  = 1080;
    let opengl = OpenGL::V3_2;
    let mut window: PistonWindow =
        WindowSettings::new("test", (x, y))
        .exit_on_esc(true)
        .graphics_api(opengl)
        .build()
        .unwrap();
    let mut texture_context = TextureContext {
        factory: window.factory.clone(),
        encoder: window.factory.create_command_buffer().into()
    };
    let mut events = Events::new(EventSettings::new().lazy(false));

    while let Some(e) = events.next(&mut window) {
        if let Some(_) = e.render_args() {
            let canvas = im::ImageBuffer::from_fn(x, y, |x, y| {
                    im::Rgba([12,13,14, 255])
            });
            let mut texture: G2dTexture = Texture::from_image(
                    &mut texture_context,
                    &canvas,
                    &TextureSettings::new()
                ).unwrap();
            texture.update(&mut texture_context, &canvas).unwrap();
            window.draw_2d(&e, |c, g, _device| {
                    image(&texture, c.transform, g);
            });
        }
    }
}

It causes memory leak like crazy.
If I comment out texture.update line, there is no leak. To reduce leak speed, change lazy from false to true.

My configuration is linux 5.7 (x86_64), nvidia-450.57-3, xserver 7.7.

Libraries:
piston = "0.49.0"
piston_window = "0.107.0"
image = "0.23.0"