From 712c80714788c7d7d877d8bb6a6f19940bc06f75 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 9 Mar 2023 21:40:37 +0300 Subject: [PATCH] Use a weak reference from WinitView to WinitWindow --- src/platform_impl/macos/view.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs index e3a8b05e9c..9bd4c5ef3e 100644 --- a/src/platform_impl/macos/view.rs +++ b/src/platform_impl/macos/view.rs @@ -7,7 +7,7 @@ use objc2::foundation::{ NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger, }; -use objc2::rc::{Id, Owned, Shared}; +use objc2::rc::{Id, Owned, Shared, WeakId}; use objc2::runtime::{Object, Sel}; use objc2::{class, declare_class, msg_send, msg_send_id, sel, ClassType}; @@ -136,7 +136,7 @@ declare_class!( #[derive(Debug)] #[allow(non_snake_case)] pub(super) struct WinitView { - _ns_window: IvarDrop>, + _ns_window: IvarDrop>>, pub(super) state: IvarDrop>, marked_text: IvarDrop>, accepts_first_mouse: bool, @@ -167,7 +167,10 @@ declare_class!( forward_key_to_app: false, }; - Ivar::write(&mut this._ns_window, window.retain()); + Ivar::write( + &mut this._ns_window, + Box::new(WeakId::new(&window.retain())), + ); Ivar::write(&mut this.state, Box::new(state)); Ivar::write(&mut this.marked_text, NSMutableAttributedString::new()); Ivar::write(&mut this.accepts_first_mouse, accepts_first_mouse); @@ -873,11 +876,11 @@ impl WinitView { // (which is incompatible with `frameDidChange:`) // // unsafe { msg_send_id![self, window] } - (*self._ns_window).clone() + self._ns_window.load().expect("view to have a window") } fn window_id(&self) -> WindowId { - WindowId(self._ns_window.id()) + WindowId(self.window().id()) } fn queue_event(&self, event: WindowEvent<'static>) {