rust-windowing / winit

Window handling library in pure Rust

Home Page:https://docs.rs/winit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

winit 0.30 panics on exit on macOS

matanui159 opened this issue · comments

Description

Using this very simple setup that just creates a single window and exits on close:

use winit::{application::ApplicationHandler, event::WindowEvent, event_loop::{EventLoop, ActiveEventLoop}, window::{Window, WindowId}};

struct App {
    window: Option<Window>,
}

impl App {
    fn new() -> Self {
        Self {
            window: None
        }
    }
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let window = event_loop.create_window(Default::default()).unwrap();
        self.window = Some(window);
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _: WindowId,
        event: WindowEvent,
    ) {
        match event {
            WindowEvent::CloseRequested => event_loop.exit(),
            _ => {},
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();
    let mut app = App::new();
    event_loop.run_app(&mut app).unwrap();
}

At exit it will panic with the following error:

thread 'main' panicked at [...]/winit-0.30.0/src/platform_impl/macos/app_delegate.rs:147:39:
a delegate was not configured on the application
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

While this isn't the biggest issue since it was already exiting, it prevents doing any checks/cleanup after the event loop has run. Using similar code for 0.29 doesn't cause any issues, and neither does using run_app_on_demand.

macOS version

ProductName:            macOS
ProductVersion:         14.4.1
BuildVersion:           23E224

Winit version

0.30.0

Thanks for the reproducer. Enabling a full backtrace reveals the issue to happen when dropping the Window, inside windowWillClose:.

This will be fixed by #3684, in the meantime, you can prevent this issue by dropping the window yourself inside exiting or the CloseRequested event:

WindowEvent::CloseRequested => {
    let _ = self.window.take();
    event_loop.exit();
}