diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp index cdb83add952f2a..c24ec473530064 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/PointerEventsProcessor.cpp @@ -12,7 +12,9 @@ namespace facebook::react { static ShadowNode::Shared getShadowNodeFromEventTarget( jsi::Runtime& runtime, const EventTarget& target) { + target.retain(runtime); auto instanceHandle = target.getInstanceHandle(runtime); + target.release(runtime); if (instanceHandle.isObject()) { auto handleObj = instanceHandle.asObject(runtime); if (handleObj.hasProperty(runtime, "stateNode")) { @@ -230,13 +232,11 @@ void PointerEventsProcessor::interceptPointerEvent( } } - eventTarget->retain(runtime); auto shadowNode = getShadowNodeFromEventTarget(runtime, *eventTarget); if (shadowNode != nullptr && shouldEmitPointerEvent(*shadowNode, type, uiManager)) { eventDispatcher(runtime, eventTarget, type, priority, pointerEvent); } - eventTarget->release(runtime); // Implicit pointer capture release if (overrideTarget != nullptr && @@ -354,7 +354,6 @@ void PointerEventsProcessor::processPendingPointerCapture( if (hasActiveOverride && activeOverrideTag != pendingOverrideTag) { auto retargeted = retargetPointerEvent(event, *activeOverride, uiManager); - retargeted.target->retain(runtime); auto shadowNode = getShadowNodeFromEventTarget(runtime, *retargeted.target); if (shadowNode != nullptr && shouldEmitPointerEvent( @@ -366,13 +365,11 @@ void PointerEventsProcessor::processPendingPointerCapture( ReactEventPriority::Discrete, retargeted.event); } - retargeted.target->release(runtime); } if (hasPendingOverride && activeOverrideTag != pendingOverrideTag) { auto retargeted = retargetPointerEvent(event, *pendingOverride, uiManager); - retargeted.target->retain(runtime); auto shadowNode = getShadowNodeFromEventTarget(runtime, *retargeted.target); if (shadowNode != nullptr && shouldEmitPointerEvent( @@ -384,7 +381,6 @@ void PointerEventsProcessor::processPendingPointerCapture( ReactEventPriority::Discrete, retargeted.event); } - retargeted.target->release(runtime); } if (!hasPendingOverride) { diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index 0baf44b8e3e2bf..61fcc37a174513 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -97,7 +97,8 @@ void UIManagerBinding::dispatchEvent( const EventPayload& eventPayload) const { SystraceSection s("UIManagerBinding::dispatchEvent", "type", type); - if (eventPayload.getType() == EventPayloadType::PointerEvent) { + if (eventTarget != nullptr && + eventPayload.getType() == EventPayloadType::PointerEvent) { auto pointerEvent = static_cast(eventPayload); auto dispatchCallback = [this]( jsi::Runtime& runtime, @@ -105,8 +106,10 @@ void UIManagerBinding::dispatchEvent( const std::string& type, ReactEventPriority priority, const EventPayload& eventPayload) { + eventTarget->retain(runtime); this->dispatchEventToJS( runtime, eventTarget, type, priority, eventPayload); + eventTarget->release(runtime); }; pointerEventsProcessor_.interceptPointerEvent( runtime,