@@ -7,7 +7,7 @@ use objc2::foundation::{
77 NSArray , NSAttributedString , NSAttributedStringKey , NSCopying , NSMutableAttributedString ,
88 NSObject , NSPoint , NSRange , NSRect , NSSize , NSString , NSUInteger ,
99} ;
10- use objc2:: rc:: { Id , Owned , Shared } ;
10+ use objc2:: rc:: { Id , Owned , Shared , WeakId } ;
1111use objc2:: runtime:: { Object , Sel } ;
1212use objc2:: { class, declare_class, msg_send, msg_send_id, sel, ClassType } ;
1313
@@ -136,7 +136,8 @@ declare_class!(
136136 #[ derive( Debug ) ]
137137 #[ allow( non_snake_case) ]
138138 pub ( super ) struct WinitView {
139- _ns_window: IvarDrop <Id <WinitWindow , Shared >>,
139+ // Weak reference because the window keeps a strong reference to the view
140+ _ns_window: IvarDrop <Box <WeakId <WinitWindow >>>,
140141 pub ( super ) state: IvarDrop <Box <ViewState >>,
141142 marked_text: IvarDrop <Id <NSMutableAttributedString , Owned >>,
142143 accepts_first_mouse: bool ,
@@ -167,7 +168,10 @@ declare_class!(
167168 forward_key_to_app: false ,
168169 } ;
169170
170- Ivar :: write( & mut this. _ns_window, window. retain( ) ) ;
171+ Ivar :: write(
172+ & mut this. _ns_window,
173+ Box :: new( WeakId :: new( & window. retain( ) ) ) ,
174+ ) ;
171175 Ivar :: write( & mut this. state, Box :: new( state) ) ;
172176 Ivar :: write( & mut this. marked_text, NSMutableAttributedString :: new( ) ) ;
173177 Ivar :: write( & mut this. accepts_first_mouse, accepts_first_mouse) ;
@@ -873,11 +877,11 @@ impl WinitView {
873877 // (which is incompatible with `frameDidChange:`)
874878 //
875879 // unsafe { msg_send_id![self, window] }
876- ( * self . _ns_window ) . clone ( )
880+ self . _ns_window . load ( ) . expect ( "view to have a window" )
877881 }
878882
879883 fn window_id ( & self ) -> WindowId {
880- WindowId ( self . _ns_window . id ( ) )
884+ WindowId ( self . window ( ) . id ( ) )
881885 }
882886
883887 fn queue_event ( & self , event : WindowEvent < ' static > ) {
0 commit comments