From 9f2fbc23e48af9be56b3729d514fbb3fff4ba376 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Fri, 15 Aug 2025 11:43:41 -0700 Subject: [PATCH] Fix memory leak in TestCallInvoker (#53287) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53287 Changelog: [General][Fixed] Fix memory leak in TestCallInvoker This fixes leaks in TestCallInvoker holding onto the jsi::Runtime Reviewed By: lenaic Differential Revision: D80295420 fbshipit-source-id: b14368ccfa86b3bf24b1f84613ec07931bd71a43 --- .../callinvoker/ReactCommon/tests/TestCallInvoker.h | 11 +++++------ .../ReactCommon/react/bridging/tests/BridgingTest.h | 2 +- .../nativemodule/core/tests/TurboModuleTestFixture.h | 2 +- .../react/io/tests/NetworkingModuleTests.cpp | 2 +- .../tests/NativeCxxModuleExampleTests.cpp | 1 - 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h b/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h index ae4d55ab1e2..98807df7cc9 100644 --- a/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h +++ b/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h @@ -10,13 +10,12 @@ #include #include #include -#include namespace facebook::react { class TestCallInvoker : public CallInvoker { public: - explicit TestCallInvoker(std::shared_ptr runtime) + explicit TestCallInvoker(facebook::jsi::Runtime& runtime) : runtime_(runtime) {} void invokeAsync(CallFunc&& func) noexcept override { @@ -24,14 +23,14 @@ class TestCallInvoker : public CallInvoker { } void invokeSync(CallFunc&& func) override { - func(*runtime_); + func(runtime_); } void flushQueue() { while (!queue_.empty()) { - queue_.front()(*runtime_); + queue_.front()(runtime_); queue_.pop_front(); - runtime_->drainMicrotasks(); // Run microtasks every cycle. + runtime_.drainMicrotasks(); // Run microtasks every cycle. } } @@ -40,8 +39,8 @@ class TestCallInvoker : public CallInvoker { } private: + facebook::jsi::Runtime& runtime_; std::list queue_{}; - std::shared_ptr runtime_{}; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h index 973fcad5b12..533feeceaca 100644 --- a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h +++ b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h @@ -31,7 +31,7 @@ class BridgingTest : public ::testing::Test { .withMicrotaskQueue(true) .build())), rt(*runtime), - invoker(std::make_shared(runtime)) {} + invoker(std::make_shared(*runtime)) {} ~BridgingTest() override { LongLivedObjectCollection::get(rt).clear(); diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/tests/TurboModuleTestFixture.h b/packages/react-native/ReactCommon/react/nativemodule/core/tests/TurboModuleTestFixture.h index abdbebdeae8..84e20686e69 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/tests/TurboModuleTestFixture.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/tests/TurboModuleTestFixture.h @@ -27,7 +27,7 @@ class TurboModuleTestFixture : public ::testing::Test { public: explicit TurboModuleTestFixture(Args... args) : runtime_(hermes::makeHermesRuntime()), - jsInvoker_(std::make_shared(runtime_)), + jsInvoker_(std::make_shared(*runtime_)), module_(std::make_shared(jsInvoker_, std::forward(args)...)) {} void SetUp() override { diff --git a/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp b/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp index 1ff920cb37f..640b0904aa8 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp +++ b/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp @@ -25,7 +25,7 @@ class NetworkingModuleTests : public testing::Test { protected: void SetUp() override { rt_ = facebook::hermes::makeHermesRuntime(); - jsInvoker_ = std::make_shared(rt_); + jsInvoker_ = std::make_shared(*rt_); } static void verifyFormData( diff --git a/packages/rn-tester/NativeCxxModuleExample/tests/NativeCxxModuleExampleTests.cpp b/packages/rn-tester/NativeCxxModuleExample/tests/NativeCxxModuleExampleTests.cpp index 616ff56fee2..c4655af63e9 100644 --- a/packages/rn-tester/NativeCxxModuleExample/tests/NativeCxxModuleExampleTests.cpp +++ b/packages/rn-tester/NativeCxxModuleExample/tests/NativeCxxModuleExampleTests.cpp @@ -6,7 +6,6 @@ */ #include -#include #include #include #include