0

I think I might have missed something about rust scoping, but I am very confused.

I thought that the below code snippets would behave exactly the same, except that in the second, the _window variable is in its own scope (which really shouldn't matter because it is never used):

let _window = WindowBuilder::new()
        .with_title("Mouse Wheel events")
        .build(&event_loop)
        .unwrap();
{
    let _window = WindowBuilder::new()
            .with_title("Mouse Wheel events")
            .build(&event_loop)
            .unwrap();
}

This is my full example using winit = "0.25":

use winit::{event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder};

fn main() {
    let event_loop = EventLoop::new();

    //{
    let _window = WindowBuilder::new()
        .with_title("Mouse Wheel events")
        .build(&event_loop)
        .unwrap();
    //}

    event_loop.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Wait;

        match event {
            Event::WindowEvent { event, .. } => match event {
                WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
                _ => (),
            },
            _ => (),
        }
    });
}

Why does the window crash, if the curly braces are commented back in?

frankenapps
  • 5,800
  • 6
  • 28
  • 69
  • 3
    With the curly braces, the window is destroyed immediately after being created. Without the braces, the window is destroyed at the end of the `main` function. – Jmb Oct 19 '21 at 12:22
  • 1
    Without looking at the code, it looks plausible that there's a hidden pointer from the event_loop to the resources hold by the window and that the disappearing of this resource on drop makes the event loop crash. A look at the relevant implementation called (X11 ? Wayland?) is probably necessary. In any case the bug isn't really in your code and an issue should be filled – Denys Séguret Oct 19 '21 at 12:23

0 Answers0