Skip to content

Fix EXC_BAD_ACCESS when clicking grouped toolbar buttons#393

Merged
schuyler merged 1 commit into
schuyler:mainfrom
yusufm:cascade/fix-toolbar-group-crash
Apr 21, 2026
Merged

Fix EXC_BAD_ACCESS when clicking grouped toolbar buttons#393
schuyler merged 1 commit into
schuyler:mainfrom
yusufm:cascade/fix-toolbar-group-crash

Conversation

@yusufm

@yusufm yusufm commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Problem

Clicking any grouped toolbar button (Shift Left/Right, Bold/Italic/Underline, H1/H2/H3, Lists) could crash the app with EXC_BAD_ACCESS. Debugging showed the invoked IBAction was entered with a nonsense sender such as 0x400000000000bad0.

Root cause

-[MPToolbarController selectedToolbarItemGroupItem:] invoked the selected toolbar item's action via a raw IMP cast:

IMP imp = [document methodForSelector:selectedItem.action];
void (*impFunc)(id) = (void *)imp;
impFunc(document);

Objective-C methods actually have the ABI signature (id self, SEL _cmd, id sender, ...). The cast to void (*)(id) only passes self, so on ARM64 the _cmd (x1) and sender (x2) registers were left holding whatever garbage the caller's frame happened to contain. That garbage surfaced as bogus sender pointers (e.g. 0x400000000000bad0) and intermittent EXC_BAD_ACCESS once the invoked action (or its responder-chain / menu validation) touched sender.

Fix

Replace the raw IMP call with performSelector:withObject:, which goes through objc_msgSend and correctly sets up self, _cmd, and the sender argument. The NSToolbarItem is passed as the sender, matching the semantics of a normal toolbar click.

Testing

Manual: clicked every grouped toolbar item (Bold, Italic, Underline, H1/H2/H3, Ordered/Unordered List, Shift Left/Right) — no more crashes; all actions take effect on the document.

selectedToolbarItemGroupItem: was invoking the selected toolbar item's
action via a raw IMP cast to void (*)(id), passing only self. Objective-C
methods actually have the ABI signature (id self, SEL _cmd, id sender, ...),
so on ARM64 the _cmd (x1) and sender (x2) registers were left with whatever
garbage the caller's frame happened to contain. This produced nonsense
senders such as 0x400000000000bad0 and intermittent EXC_BAD_ACCESS crashes
when the invoked IBAction (or its responder-chain / menu validation) touched
sender.

Replace the raw IMP call with performSelector:withObject:, which goes through
objc_msgSend and correctly sets up self, _cmd, and sender. The NSToolbarItem
is passed as the sender, matching the semantics of a normal toolbar click.

Copy link
Copy Markdown
Owner

Thanks for the PR, @yusufm! Nicely tracked down — the ARM64 ABI mismatch explanation in the PR description is a great reference for anyone who hits a similar issue. Merging.


Generated by Claude Code

@schuyler schuyler merged commit 0235d41 into schuyler:main Apr 21, 2026
5 checks passed
@yusufm yusufm deleted the cascade/fix-toolbar-group-crash branch April 21, 2026 05:52
@schuyler schuyler added the rc-pending Included in a release candidate awaiting validation label Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rc-pending Included in a release candidate awaiting validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants