From ba72ca65c2bf648f1c930224d847ee9f787a8791 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Thu, 24 Aug 2023 19:43:23 -0700 Subject: [PATCH] Fix crash in CompositeTurboModuleManagerDelegate (#39155) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39155 [Changelog]: [Android][Fixed] Fix crash in CompositeTurboModuleManagerDelegate CompositeTurboModuleManagerDelegate.cpp crashes as we pass in one or multiple ``` jni::alias_ref delegate ``` but don't retain a global reference to them. Fix use: ``` jni::make_global(delegate) ``` Differential Revision: D48670217 fbshipit-source-id: 51e4717187d8b0bdc016b1107960a7911ada163d --- .../CompositeTurboModuleManagerDelegate.cpp | 11 ++++++----- .../ReactCommon/CompositeTurboModuleManagerDelegate.h | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.cpp index 4796310bca7277..a0c76fcbcb063a 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.cpp @@ -29,7 +29,8 @@ CompositeTurboModuleManagerDelegate::getTurboModule( const std::string &moduleName, const std::shared_ptr &jsInvoker) { for (auto delegate : mDelegates_) { - if (auto turboModule = delegate->getTurboModule(moduleName, jsInvoker)) { + if (auto turboModule = + delegate->cthis()->getTurboModule(moduleName, jsInvoker)) { return turboModule; } } @@ -41,7 +42,8 @@ CompositeTurboModuleManagerDelegate::getTurboModule( const std::string &moduleName, const JavaTurboModule::InitParams ¶ms) { for (auto delegate : mDelegates_) { - if (auto turboModule = delegate->getTurboModule(moduleName, params)) { + if (auto turboModule = + delegate->cthis()->getTurboModule(moduleName, params)) { return turboModule; } } @@ -49,9 +51,8 @@ CompositeTurboModuleManagerDelegate::getTurboModule( } void CompositeTurboModuleManagerDelegate::addTurboModuleManagerDelegate( - jni::alias_ref - turboModuleManagerDelegate) { - mDelegates_.insert(turboModuleManagerDelegate->cthis()); + jni::alias_ref delegate) { + mDelegates_.push_back(jni::make_global(delegate)); } } // namespace facebook::react diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.h b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.h index 46aece570b1a6d..9f4fdb6c42cfa8 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/CompositeTurboModuleManagerDelegate.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include namespace facebook::react { @@ -30,6 +30,7 @@ class CompositeTurboModuleManagerDelegate std::shared_ptr getTurboModule( const std::string &moduleName, const std::shared_ptr &jsInvoker) override; + std::shared_ptr getTurboModule( const std::string &moduleName, const JavaTurboModule::InitParams ¶ms) override; @@ -37,11 +38,11 @@ class CompositeTurboModuleManagerDelegate private: friend HybridBase; using HybridBase::HybridBase; - std::unordered_set mDelegates_; + std::vector> + mDelegates_; void addTurboModuleManagerDelegate( - jni::alias_ref - turboModuleManagerDelegate); + jni::alias_ref delegate); }; } // namespace facebook::react