Repository navigation
Fix Switch crash when onChange fires after view recycle - #59216
Mr-Perfectuz wants to merge 1 commit into
Conversation
Guard _eventEmitter in RCTSwitchComponentView onChange before dispatch, matching ScrollView/Image/Modal. prepareForRecycle clears the emitter while UISwitch can still deliver a deferred ValueChanged. Fixes react#59203 Co-authored-by: Cursor <cursoragent@cursor.com>
|
Hi @Mr-Perfectuz! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
done CLA |
|
Thanks for the fix. The issue says the crash can't be reproduced on demand, so I wrote a unit test that hits this code path. It may help this PR get merged. It fails on
The test calls the action directly. Unit tests run without
|
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary:
Fixes #59203.
-[RCTSwitchComponentView onChange:]dereferences_eventEmitterwithout checking it.RCTViewComponentView -prepareForRecycleresets_eventEmitter, and on iOS 26+ the redesignedUISwitchcan deliver itsValueChangedaction 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 withEXC_BAD_ACCESSinsideEventEmitter::dispatchEvent.This adds an early return when
_eventEmitteris null, matching the existing guards inRCTScrollViewComponentView,RCTImageComponentView,RCTModalHostViewComponentViewandRCTVirtualViewComponentView. Dropping the event is correct since the recycled view no longer represents that React node.Changelog:
[IOS] [FIXED] - Fix crash in
SwitchwhenonChangefires after the view has been recycledTest Plan:
_eventEmitteris set for mounted views.🤖 Generated with Claude Code