Skip to content

Commit

Permalink
Fix crash in CompositeTurboModuleManagerDelegate (#39155)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39155

[Changelog]: [Android][Fixed] Fix crash in CompositeTurboModuleManagerDelegate

CompositeTurboModuleManagerDelegate.cpp crashes as we pass in one or multiple
```
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate
```
but don't retain a global reference to them. Fix use:
```
jni::make_global(delegate)
```

Differential Revision: D48670217

fbshipit-source-id: e805f28919918b2a867589dbb6e2a70d67dcd8c3
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Aug 25, 2023
1 parent 5cfa125 commit e716459
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ CompositeTurboModuleManagerDelegate::getTurboModule(
const std::string &moduleName,
const std::shared_ptr<CallInvoker> &jsInvoker) {
for (auto delegate : mDelegates_) {
if (auto turboModule = delegate->getTurboModule(moduleName, jsInvoker)) {
if (auto turboModule =
delegate->cthis()->getTurboModule(moduleName, jsInvoker)) {
return turboModule;
}
}
Expand All @@ -41,17 +42,17 @@ CompositeTurboModuleManagerDelegate::getTurboModule(
const std::string &moduleName,
const JavaTurboModule::InitParams &params) {
for (auto delegate : mDelegates_) {
if (auto turboModule = delegate->getTurboModule(moduleName, params)) {
if (auto turboModule =
delegate->cthis()->getTurboModule(moduleName, params)) {
return turboModule;
}
}
return nullptr;
}

void CompositeTurboModuleManagerDelegate::addTurboModuleManagerDelegate(
jni::alias_ref<TurboModuleManagerDelegate::javaobject>
turboModuleManagerDelegate) {
mDelegates_.insert(turboModuleManagerDelegate->cthis());
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate) {
mDelegates_.push_back(jni::make_global(delegate));
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <fbjni/fbjni.h>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>

namespace facebook::react {

Expand All @@ -30,18 +30,19 @@ class CompositeTurboModuleManagerDelegate
std::shared_ptr<TurboModule> getTurboModule(
const std::string &moduleName,
const std::shared_ptr<CallInvoker> &jsInvoker) override;

std::shared_ptr<TurboModule> getTurboModule(
const std::string &moduleName,
const JavaTurboModule::InitParams &params) override;

private:
friend HybridBase;
using HybridBase::HybridBase;
std::unordered_set<TurboModuleManagerDelegate *> mDelegates_;
std::vector<jni::global_ref<TurboModuleManagerDelegate::javaobject>>
mDelegates_;

void addTurboModuleManagerDelegate(
jni::alias_ref<TurboModuleManagerDelegate::javaobject>
turboModuleManagerDelegate);
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate);
};

} // namespace facebook::react

0 comments on commit e716459

Please sign in to comment.