Skip to content

fix: prevent crash on __weak NSWindow ref from JNI thread during disposal#100

Merged
kdroidFilter merged 1 commit intomainfrom
fix/macos-jni-weak-ref-crash
Mar 10, 2026
Merged

fix: prevent crash on __weak NSWindow ref from JNI thread during disposal#100
kdroidFilter merged 1 commit intomainfrom
fix/macos-jni-weak-ref-crash

Conversation

@kdroidFilter
Copy link
Owner

@kdroidFilter kdroidFilter commented Mar 10, 2026

Summary

  • nativeRemoveMenuBarMonitor and nativeResetTitleBar were creating __weak NSWindow* references on the AWT-EventQueue Java thread via objc_initWeak, which accesses the object's memory — crashing with EXC_BAD_ACCESS if the NSWindow was already deallocated on the main thread
  • Replaced with raw pointer capture + dispatch_async to main thread, validating the window is still alive via [NSApp windows] pointer comparison before accessing it
  • This is a follow-up to fix: prevent crash on NSWindow cleanup during fullscreen #97 which fixed a similar cleanup crash during fullscreen transitions

Test plan

  • Launch the app and close the window rapidly — no crash
  • Enter/exit fullscreen repeatedly while closing — no crash
  • Verify menu bar monitor installs/removes correctly during fullscreen on non-notch displays
  • Verify title bar reset works correctly on window close

…osal

nativeRemoveMenuBarMonitor and nativeResetTitleBar create a __weak
NSWindow reference on the AWT-EventQueue thread. If the NSWindow has
already been deallocated on the main thread, objc_initWeak accesses
freed memory causing EXC_BAD_ACCESS (SIGABRT).

Instead, capture the raw pointer and dispatch to the main thread,
then validate the window is still alive via [NSApp windows] before
accessing it. Pointer comparison is safe even for freed pointers.
@kdroidFilter kdroidFilter merged commit 2239cf3 into main Mar 10, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant