Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MacOS] Close a invisible window cause segment fault. #1863

Closed
kaiwk opened this issue Feb 18, 2021 · 4 comments
Closed

[MacOS] Close a invisible window cause segment fault. #1863

kaiwk opened this issue Feb 18, 2021 · 4 comments

Comments

@kaiwk
Copy link
Contributor

kaiwk commented Feb 18, 2021

The code is modified from examples/multiwindow.rs, and reproduction is simple:

  1. press 'a' will add an invisible window;
  2. press 'x' will remove all windows except current window (which receives key events), then a segment fault throwed.
use std::collections::HashMap;

use simple_logger::SimpleLogger;
use winit::{
    event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::Window,
};

fn main() {
    SimpleLogger::new().init().unwrap();
    let event_loop = EventLoop::new();

    let mut windows = HashMap::new();
    let window = Window::new(&event_loop).unwrap();
    windows.insert(window.id(), window);

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

        match event {
            Event::WindowEvent { event, window_id } => {
                match event {
                    WindowEvent::CloseRequested => {
                        println!("Window {:?} has received the signal to close", window_id);

                        // This drops the window, causing it to close.
                        windows.remove(&window_id);

                        if windows.is_empty() {
                            *control_flow = ControlFlow::Exit;
                        }
                    }
                    WindowEvent::KeyboardInput {
                        input:
                            KeyboardInput {
                                state: ElementState::Pressed,
                                virtual_keycode: Some(VirtualKeyCode::A),
                                ..
                            },
                        ..
                    } => {
                        let window = Window::new(&event_loop).unwrap();
                        window.set_visible(false);
                        windows.insert(window.id(), window);
                    }
                    WindowEvent::KeyboardInput {
                        input:
                            KeyboardInput {
                                state: ElementState::Pressed,
                                virtual_keycode: Some(VirtualKeyCode::X),
                                ..
                            },
                        ..
                    } => {
                        windows.retain(|id, _| *id == window_id);
                    }
                    _ => (),
                }
            }
            _ => (),
        }
    })
}

I guess it's related to #158?

@kaiwk
Copy link
Contributor Author

kaiwk commented Feb 18, 2021

From the nswindow doc, even for invisible window, nswindow.close() should post NSWindowWillCloseNotification, but looks like window_will_close is not triggtered from the trace log.

For example, when the application terminates, it sends the close message to all windows in its window list, even those that are not currently visible.

@kaiwk
Copy link
Contributor Author

kaiwk commented Feb 18, 2021

It seems window delegate drops before NSWindow somehow, one quick dirty fix is here.

@ArturKovacs
Copy link
Contributor

Please check if this is fixed on the master currently. I believe this was fixed by #1874.

@kaiwk
Copy link
Contributor Author

kaiwk commented Mar 15, 2021

Thanks, I tested it and it works.

@kaiwk kaiwk closed this as completed Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants