From 2a1e12f7873ab5ea973011402e3af40c978f6895 Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Fri, 7 Nov 2025 13:13:02 -0800 Subject: [PATCH] add dealloc function to RCTAnimatedModuleProvider (#54440) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54440 ## Changelog: [Internal] [Changed] - add dealloc function to RCTAnimatedModuleProvider just in case RCTAnimatedModuleProvider is destroyed earlier than AnimatedModule when react instance tears down, then when AnimatedModule (which owns NativeAnimatedNodesManager) is destroyed, even if StopOnRenderCallback is invoked, it doesn't invalidate displayLink (since strongSelf is nullptr) Reviewed By: javache Differential Revision: D86459439 fbshipit-source-id: 707bbe91228b9c062dfbb324fedca1914663650b --- .../RCTAnimatedModuleProvider.mm | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm index 6dfee4465e6..772dbf7f40d 100644 --- a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm +++ b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm @@ -26,6 +26,26 @@ std::function _onRender; } +- (void)dealloc +{ + if (_displayLink != nil) { +#if TARGET_OS_OSX + RCTPlatformDisplayLink *displayLink = _displayLink; +#else + CADisplayLink *displayLink = _displayLink; +#endif + _displayLink = nil; + if ([NSThread isMainThread]) { + [displayLink invalidate]; + } else { + dispatch_sync(dispatch_get_main_queue(), ^{ + [displayLink invalidate]; + }); + } + _onRender = nullptr; + } +} + - (void)_onDisplayLinkTick { if (_displayLink != nullptr && _onRender != nullptr) { @@ -69,11 +89,16 @@ const auto stop_render = [weakSelf]() { RCTAnimatedModuleProvider *strongSelf = weakSelf; if (strongSelf) { - if (strongSelf->_displayLink != nil) { - [strongSelf->_displayLink invalidate]; - strongSelf->_displayLink = nil; - strongSelf->_onRender = nullptr; +#if TARGET_OS_OSX + RCTPlatformDisplayLink *displayLink = strongSelf->_displayLink; +#else + CADisplayLink *displayLink = strongSelf->_displayLink; +#endif + strongSelf->_displayLink = nil; + if (displayLink != nil) { + [displayLink invalidate]; } + strongSelf->_onRender = nullptr; } };