diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 7b17e13c25c..4e461d22663 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 91cab6bfb06..38677ebb873 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