Skip to content

Commit 7612536

Browse files
anvarbekmeta-codesync[bot]
authored andcommitted
Fix Switch crash when onChange fires after view recycle (#59216)
Summary: Fixes #59203. `-[RCTSwitchComponentView onChange:]` dereferences `_eventEmitter` without checking it. `RCTViewComponentView -prepareForRecycle` resets `_eventEmitter`, and on iOS 26+ the redesigned `UISwitch` can deliver its `ValueChanged` action from a deferred `_UIAfterCACommitBlock` (e.g. after a drag/long-press). If the view gets recycled in between, `onChange:` runs on a recycled view and crashes with `EXC_BAD_ACCESS` inside `EventEmitter::dispatchEvent`. This adds an early return when `_eventEmitter` is null, matching the existing guards in `RCTScrollViewComponentView`, `RCTImageComponentView`, `RCTModalHostViewComponentView` and `RCTVirtualViewComponentView`. Dropping the event is correct since the recycled view no longer represents that React node. ## Changelog: [IOS] [FIXED] - Fix crash in `Switch` when `onChange` fires after the view has been recycled Pull Request resolved: #59216 Test Plan: - The crash is intermittent and timing-dependent (reported from production), so it is not reproducible on demand. - The change is limited to an early-return null guard before the emitter dereference; normal toggling in RNTester's Switch examples is unaffected since `_eventEmitter` is set for mounted views. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: cipolleschi Differential Revision: D126404965 Pulled By: javache fbshipit-source-id: 5529c2f8d56cd9f9db5ea9f722e9e15794e2e8c1
1 parent 0bd3326 commit 7612536

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

‎packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ - (void)onChange:(UISwitch *)sender
110110
return;
111111
}
112112

113+
if (!_eventEmitter) {
114+
return;
115+
}
116+
113117
static_cast<const SwitchEventEmitter &>(*_eventEmitter)
114118
.onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
115119
}

0 commit comments

Comments
 (0)