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

Fix panic when destroying window #2773

Merged

Conversation

xiaopengli89
Copy link
Contributor

@xiaopengli89 xiaopengli89 commented Apr 19, 2023

  • Tested on all platforms changed
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users

Dropping window may cause panic.

thread 'main' panicked at 'view to have a window', src/platform_impl/macos/view.rs:880:32

@xiaopengli89 xiaopengli89 requested a review from madsmtm as a code owner April 19, 2023 13:36
@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from ac99eeb to e8233f2 Compare May 9, 2023 02:53
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I would have thought this to be impossible, since view methods should not be called if the view is no longer attached to a window?

Are you encountering this in multi-threaded code? If so, I suspect that may be the real culprit instead?

@xiaopengli89
Copy link
Contributor Author

Here is a minimum reproducible example:

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

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

    let mut window = WindowBuilder::new()
        .with_title("A fantastic window!")
        .with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
        .build(&event_loop)
        .unwrap();

    event_loop.run(move |event, target, control_flow| {
        control_flow.set_wait();

        match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                window_id: _,
            } => control_flow.set_exit(),
            Event::MainEventsCleared => {
                window.request_redraw();

                window = WindowBuilder::new()
                    .with_title("A fantastic window!")
                    .with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
                    .build(target)
                    .unwrap();
            }
            _ => (),
        }
    });
}

@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from e8233f2 to 9f8aba5 Compare May 10, 2023 05:16
@xiaopengli89 xiaopengli89 requested a review from madsmtm May 10, 2023 08:58
@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from 9f8aba5 to 26a2976 Compare May 13, 2023 11:12
@kchibisov
Copy link
Member

This will need a rebase.

ping @madsmtm for re-review

@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from 26a2976 to 60ca3e0 Compare May 30, 2023 14:41
@xiaopengli89
Copy link
Contributor Author

Done.

@madsmtm
Copy link
Member

madsmtm commented Jun 5, 2023

Hmm, I cannot reproduce from your example? Is it still an issue for you?

Maybe you can add #[track_caller] to window and window_id, or post the full stack-trace, so that we can see which call is the real culprit?

@xiaopengli89
Copy link
Contributor Author

Hmm, I cannot reproduce from your example? Is it still an issue for you?

Maybe you can add #[track_caller] to window and window_id, or post the full stack-trace, so that we can see which call is the real culprit?

[05:25 pm] winit on  master [!]
❯ let-env RUST_BACKTRACE = 1

[05:25 pm] winit on  master [!]
❯ cargo r --example window
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
     Running `target/debug/examples/window`
thread 'main' panicked at 'view to have a window', src/platform_impl/macos/view.rs:811:32
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/std/src/panicking.rs:593:5
   1: core::panicking::panic_fmt
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/panicking.rs:67:14
   2: core::panicking::panic_display
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/panicking.rs:150:5
   3: core::panicking::panic_str
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/panicking.rs:134:5
   4: core::option::expect_failed
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/option.rs:1932:5
   5: core::option::Option<T>::expect
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/option.rs:898:21
   6: winit::platform_impl::platform::view::WinitView::window
             at ./src/platform_impl/macos/view.rs:811:9
   7: winit::platform_impl::platform::view::WinitView::window_id
             at ./src/platform_impl/macos/view.rs:815:18
   8: winit::platform_impl::platform::view::WinitView::draw_rect
             at ./src/platform_impl/macos/view.rs:246:37
   9: <unknown>
  10: <unknown>
  11: <unknown>
  12: <unknown>
  13: <unknown>
  14: <unknown>
  15: <unknown>
  16: <unknown>
  17: <unknown>
  18: <unknown>
  19: <unknown>
  20: <unknown>
  21: <unknown>
  22: <unknown>
  23: <unknown>
  24: <unknown>
  25: <unknown>
  26: <unknown>
  27: <unknown>
  28: <unknown>
  29: <unknown>
  30: <unknown>
  31: <unknown>
  32: <() as objc2::message::MessageArguments>::__invoke
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/message/mod.rs:390:26
  33: objc2::message::platform::send_unverified::{{closure}}
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/message/apple/mod.rs:36:33
  34: objc2::message::conditional_try
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/message/mod.rs:30:5
  35: objc2::message::platform::send_unverified
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/message/apple/mod.rs:36:14
  36: objc2::message::MessageReceiver::send_message
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/message/mod.rs:195:46
  37: winit::platform_impl::platform::appkit::application::NSApplication::run
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/macros/__rewrite_self_arg.rs:42:21
  38: winit::platform_impl::platform::event_loop::EventLoop<T>::run_return::{{closure}}
             at ./src/platform_impl/macos/event_loop.rs:220:22
  39: objc2::rc::autorelease::autoreleasepool
             at /Users/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.3.0-beta.3/src/rc/autorelease.rs:313:5
  40: winit::platform_impl::platform::event_loop::EventLoop<T>::run_return
             at ./src/platform_impl/macos/event_loop.rs:211:25
  41: winit::platform_impl::platform::event_loop::EventLoop<T>::run
             at ./src/platform_impl/macos/event_loop.rs:190:25
  42: winit::event_loop::EventLoop<T>::run
             at ./src/event_loop.rs:308:9
  43: window::main
             at ./examples/window.rs:16:5
  44: core::ops::function::FnOnce::call_once
             at /rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! Now I'm wondering why drawRect: is being called!

May be part of #2640, since we're handling redraws kinda badly right now?
If you only change drawRect: to be something like the following, does the issue appear elsewhere?

trace_scope!("drawRect:");
let Some(window) = self._ns_window.load() {
    AppState::handle_redraw(window.id());
}
let _: () = unsafe { msg_send![super(self), drawRect: rect] };

And maybe you could enable tracing as well, would be nice for me to see the events you're hitting before the crash.

src/platform_impl/macos/view.rs Outdated Show resolved Hide resolved
@xiaopengli89
Copy link
Contributor Author

If you only change drawRect: to be something like the following, does the issue appear elsewhere?

The issue resolved, so is it a better solution?

@madsmtm
Copy link
Member

madsmtm commented Jun 6, 2023

I think I would prefer that, since I suspect it is something that ties in with the fix for #2640.

@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from 60ca3e0 to 31e3606 Compare June 7, 2023 09:12
@xiaopengli89
Copy link
Contributor Author

Done.

@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch 2 times, most recently from 49126d1 to 01b8d09 Compare June 7, 2023 09:15
@xiaopengli89 xiaopengli89 requested a review from madsmtm June 7, 2023 10:01
@xiaopengli89 xiaopengli89 force-pushed the fix-crash-when-destroying branch from 01b8d09 to dea2f3c Compare June 8, 2023 06:14
@xiaopengli89 xiaopengli89 requested a review from madsmtm June 8, 2023 12:20
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@madsmtm madsmtm merged commit 07d39ab into rust-windowing:master Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants