From d6f7a28499ef59c04980022567ca608fa82a24dd Mon Sep 17 00:00:00 2001 From: Jeremiah S <46010924+HXavS@users.noreply.github.com> Date: Thu, 14 Aug 2025 19:52:23 -0500 Subject: [PATCH] macOS: fix crash due during window drop On macOS 26+ the window drop was leading to unwrap, since events were coming after the window was already destroyed, while it sounds rather strange, guard against such things just in case. Fixes #4333. --- winit-appkit/src/view.rs | 12 ++++++++++-- winit/src/changelog/unreleased.md | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/winit-appkit/src/view.rs b/winit-appkit/src/view.rs index 8f8976e9c..dc57fa7dc 100644 --- a/winit-appkit/src/view.rs +++ b/winit-appkit/src/view.rs @@ -12,6 +12,7 @@ use objc2_app_kit::{ NSApplication, NSCursor, NSEvent, NSEventPhase, NSResponder, NSTextInputClient, NSTrackingRectTag, NSView, NSWindow, }; +use objc2_core_foundation::CGRect; use objc2_foundation::{ NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString, NSNotFound, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger, @@ -361,9 +362,16 @@ define_class!( _actual_range: *mut NSRange, ) -> NSRect { trace_scope!("firstRectForCharacterRange:actualRange:"); + + // Guard when the view is no longer in a window during teardown. + let Some(window) = (**self).window() else { + return CGRect::ZERO; + }; + + // Return value is expected to be in screen coordinates, so we need a conversion let rect = NSRect::new(self.ivars().ime_position.get(), self.ivars().ime_size.get()); - // Return value is expected to be in screen coordinates, so we need a conversion here - self.window().convertRectToScreen(self.convertRect_toView(rect, None)) + let view_rect = self.convertRect_toView(rect, None); + window.convertRectToScreen(view_rect) } #[unsafe(method(insertText:replacementRange:))] diff --git a/winit/src/changelog/unreleased.md b/winit/src/changelog/unreleased.md index 2976a981b..65fa6219c 100644 --- a/winit/src/changelog/unreleased.md +++ b/winit/src/changelog/unreleased.md @@ -254,3 +254,4 @@ changelog entry. - On macOS, fixed the scancode conversion for `IntlBackslash`. - On macOS, fixed redundant `SurfaceResized` event at window creation. - On macOS, don't panic on monitors with unknown bit-depths. +- On macOS, fixed crash when closing the window on macOS 26+.