FIX to piston_window segmentation fault for multi_window.rs example

I was getting a segmentation fault when running the multi_window.rs example from this URL: https://github.com/PistonDevelopers/piston-examples/tree/master/src under Ubuntu 18.04 64 bit OS.

When I tried switching the backend to sdl2 it gave me a "cannot make window more than once " error.

When trying the glfw backend it worked!!

Here are the code changes required for it to work:

in cargo.toml place these dependencies:

[dependencies]
piston_window = "0.110.0"
pistoncore-glfw_window = "0.67.0"

In multi_window.rs change the code to:

extern crate piston_window;

use glfw_window::GlfwWindow;
use piston_window::*;

fn create_window(number: usize) -> PistonWindow<GlfwWindow> {
    WindowSettings::new(format!("window {}", number + 1), [256, 256])
        .exit_on_esc(true).build().unwrap()
}

fn main() {
    let mut windows: Vec<_> = (0..3 as usize).into_iter().map(|n|
        create_window(n).position([100 + n as i32 * 300, 100])).collect();
    let colors = vec![[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]];

    loop {
        let mut any_window_open = false;

        for (i, window) in windows.iter_mut().enumerate() {
            if let Some(e) = window.next() {
                any_window_open = true;
                window.draw_2d(&e, |_c, g, _device| {
                    clear(colors[i], g);
                });
            }
            if window.should_close() { window.hide() }
        }

        if !any_window_open { break }
    }
}

I don't know, but since you have no unsafe block, it is a bug that you should report to piston_window.

Ok

FIXED by above code.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.