Skip to content

fix: Don't reset the view's class on macOS when KVO has subclassed it - #822

Open
RXNova wants to merge 1 commit into
AccessKit:mainfrom
RXNova:fix/macos-kvo-subclass-teardown
Open

RXNova wants to merge 1 commit into
AccessKit:mainfrom
RXNova:fix/macos-kvo-subclass-teardown

Conversation

@RXNova

@RXNova RXNova commented Oct 11, 2026 •

Copy link
Copy Markdown

Closes #823

SubclassingAdapter::drop resets the view's class unconditionally. If a key-value observer was registered on the view after the adapter was created, this discards KVO's NSKVONotifying_… subclass while the observer is still registered, and the later removeObserver:forKeyPath: raises NSRangeException. AppKit registers such observers itself on the first responder, so apps crash when their window is closed.

With this change, the previous class is only restored if ours is still the view's class. Otherwise the view keeps our subclass and its associated object until it is deallocated.

The new test adds an observer, drops the adapter and removes the observer. It has to run on the main thread, so it uses harness = false; tell me if you'd prefer it done differently. cargo test -p accesskit_macos passes on Rust 1.87 and on stable. I also tested manually with the same change applied to accesskit_macos 0.26.3 on macOS 27.0.1: the crash no longer happens.

@RXNova
RXNova force-pushed the fix/macos-kvo-subclass-teardown branch 3 times, most recently from f4eeeaa to 1b5351e Compare October 11, 2026 23:26
Dropping the subclassing adapter while a key-value observer is registered
on the view discarded KVO's dynamic subclass, so removing the observer
later raised NSRangeException. AppKit registers such observers itself on
the first responder, which made apps crash when closing a window.
@RXNova
RXNova force-pushed the fix/macos-kvo-subclass-teardown branch from 1b5351e to 3740c92 Compare October 11, 2026 23:32

This branch has not been deployed

No deployments
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.

Crash on macOS when the subclassing adapter is dropped while the view has key-value observers

1 participant