Window Closes Immediately
RKennedy9064 opened this issue · comments
I went through the first tutorial and seem to have run into a weird issue. When I run the code, the window appears and then immediately closes. I thought I might have typed something wrong, so I copy pasted the code from here and the same thing happens. If I move the window code into main like this, the window appears and works correctly.
fn main() {
let mut events_loop = EventsLoop::new();
let _window = WindowBuilder::new()
.with_title("Vulkan")
.with_dimensions(LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)))
.build(&events_loop);
loop {
let mut done = false;
events_loop.poll_events(|ev| {
if let Event::WindowEvent { event: WindowEvent::CloseRequested, .. } = ev {
done = true
}
});
if done {
return;
}
}
}
The issue seems to occur when I create the events_loop inside of a struct and try to run it from within there. I'm not sure how helpful it is, but I decided to print out the events inside poll_events and it looks like the window is destroyed pretty early on.
WindowEvent { window_id: WindowId(WindowId(0x3f071e)), event: Focused(true) }
WindowEvent { window_id: WindowId(WindowId(0x3f071e)), event: CursorMoved { device_id: DeviceId(DeviceId(0)), position: LogicalPosition { x: 0.0, y: 0.0 }, modifiers: ModifiersState { shift: false, ctrl: false, alt: false, logo: false } } }
WindowEvent { window_id: WindowId(WindowId(0x3f071e)), event: Resized(LogicalSize { width: 800.0, height: 600.0 }) }
WindowEvent { window_id: WindowId(WindowId(0x3f071e)), event: Focused(false) }
WindowEvent { window_id: WindowId(WindowId(0x3f071e)), event: Destroyed }
The app continues to run after the Destroyed event and doesn't end until I forcefully close it. Have you ever encountered anything like this before? Any help would be greatly appreciated.
I think the issue is that _window
is an ignored variable, and so is immediately dropped. winit closes windows when they're dropped, so try renaming the variable to just window
and see if that helps.
I tried changing the variable name to window instead of _window and it's still closing as soon as the program launches.
Are you storing the window inside the struct? Make sure it's not being dropped early.
So it looks like if I save a reference to the window inside the struct, the window renders properly, which makes sense that it would be destroyed if there's no reference to it. Out of curiosity, does the code from the base code work properly for you? I notice it only stores the events_loop inside the original struct, and doesn't save a reference to a window until chapter 5 when it stores an Arc<Window> in the struct. I'm surprised no one else has had this issue before.
No, the base code doesn't work as-is, you have to store the window to make things work. I ended up skipping ahead & figuring the code out as I went, rather than going step-by-step. This would be a good thing to fix though.
So it looks like if I save a reference to the window inside the struct, the window renders properly, which makes sense that it would be destroyed if there's no reference to it. Out of curiosity, does the code from the base code work properly for you? I notice it only stores the events_loop inside the original struct, and doesn't save a reference to a window until chapter 5 when it stores an Arc in the struct. I'm surprised no one else has had this issue before.
So how do you solve the problem?
@readlnh You just have to store a reference to the window inside the application struct like they do in the chapter 5 code. Here's what I did for a quick fix until the actual surface is introduced in later chapters.
struct HelloTriangleApplication {
events_loop: EventsLoop,
window: Window,
}
impl HelloTriangleApplication {
pub fn initialize() -> Self {
let (events_loop, window) = Self::init_window();
Self {
events_loop,
window,
}
}
fn init_window() -> (EventsLoop, Window) {
let events_loop = EventsLoop::new();
let window = WindowBuilder::new()
.with_title("Vulkan")
.with_dimensions(LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)))
.build(&events_loop)
.unwrap();
(events_loop, window)
}
}
Once you store the window in your struct it doesn't destroy itself after it leaves the function.