From 64fa2502bc1a2327d066659d58797f05a258aece Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Fri, 4 Dec 2026 11:23:44 -0800 Subject: [PATCH] Unregister AnimationBackendCommitHook in ~AnimationBackend (#60523) Summary: `AnimationBackend` registers `commitHook_` with `UIManager` in its constructor, but `~AnimationBackend()` previously did not unregister it. If `AnimationBackend` is destroyed while `UIManager` is still alive, `UIManager::commitHooks_` retains a dangling pointer that crashes on the next `ShadowTree` commit. Unregister `commitHook_` from `UIManager` in `~AnimationBackend()` when `uiManager_` is still alive. Changelog: [General][Fixed] - Unregister `AnimationBackendCommitHook` when `AnimationBackend` is destroyed Signed-off-by: Peter Abbondanzo Reviewed By: javache Differential Revision: D136090145 --- .../animationbackend/AnimationBackend.cpp | 6 ++- .../tests/AnimationBackendTest.cpp | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 7b17e13c25ce..4e461d226637 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -107,7 +107,11 @@ AnimationBackend::AnimationBackend( } } -AnimationBackend::~AnimationBackend() = default; +AnimationBackend::~AnimationBackend() { + if (auto uiManager = uiManager_.lock()) { + uiManager->unregisterCommitHook(*commitHook_); + } +} void AnimationBackend::unpackMutations( AnimationMutations& mutations, diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp index 91cab6bfb06e..38677ebb873e 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp @@ -259,4 +259,44 @@ TEST_F( localUiManager->setDelegate(nullptr); } +TEST_F(AnimationBackendTest, destroyingBackendUnregistersCommitHook) { + constexpr SurfaceId kSurfaceId = 2; + std::shared_ptr initialRoot; + builder.build( + Element() + .surfaceId(kSurfaceId) + .tag(1) + .reference(initialRoot)); + + auto localUiManager = std::make_shared( + [](std::function&& /*callback*/) {}, + contextContainer); + RecordingUIManagerDelegate localDelegate; + localUiManager->setDelegate(&localDelegate); + + auto shadowTree = std::make_unique( + kSurfaceId, + LayoutConstraints{}, + LayoutContext{}, + *localUiManager, + *contextContainer); + auto* shadowTreePtr = shadowTree.get(); + localUiManager->startEmptySurface(std::move(shadowTree)); + + { + auto temporaryBackend = std::make_shared( + std::make_shared(), localUiManager); + } + + auto status = shadowTreePtr->commit( + [&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) { + return initialRoot; + }, + {.source = ShadowTreeCommitSource::React}); + EXPECT_EQ(status, ShadowTree::CommitStatus::Succeeded); + + localUiManager->stopSurface(kSurfaceId); + localUiManager->setDelegate(nullptr); +} + } // namespace facebook::react