Skip to content

Commit

Permalink
Fix JSCRuntime destroyed with a dangling API object (#3185)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmccall committed Apr 20, 2022
1 parent e4bc507 commit 68f0b17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 22 additions & 12 deletions android/src/main/cpp/NativeProxy.cpp
Expand Up @@ -37,6 +37,13 @@ NativeProxy::NativeProxy(
scheduler_(scheduler),
layoutAnimations(std::move(_layoutAnimations)) {}

NativeProxy::~NativeProxy() {
runtime_->global().setProperty(
*runtime_,
jsi::PropNameID::forAscii(*runtime_, "__reanimatedModuleProxy"),
jsi::Value::undefined());
}

jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
jni::alias_ref<jhybridobject> jThis,
jlong jsContext,
Expand Down Expand Up @@ -192,18 +199,21 @@ void NativeProxy::installJSIBindings() {
platformDepMethodsHolder);

_nativeReanimatedModule = module;

this->registerEventHandler([module, getCurrentTime](
std::string eventName,
std::string eventAsString) {
jsi::Object global = module->runtime->global();
jsi::String eventTimestampName =
jsi::String::createFromAscii(*module->runtime, "_eventTimestamp");
global.setProperty(*module->runtime, eventTimestampName, getCurrentTime());
module->onEvent(eventName, eventAsString);
global.setProperty(
*module->runtime, eventTimestampName, jsi::Value::undefined());
});
std::weak_ptr<NativeReanimatedModule> weakModule = module;
this->registerEventHandler(
[weakModule, getCurrentTime](
std::string eventName, std::string eventAsString) {
if (auto module = weakModule.lock()) {
jsi::Object global = module->runtime->global();
jsi::String eventTimestampName =
jsi::String::createFromAscii(*module->runtime, "_eventTimestamp");
global.setProperty(
*module->runtime, eventTimestampName, getCurrentTime());
module->onEvent(eventName, eventAsString);
global.setProperty(
*module->runtime, eventTimestampName, jsi::Value::undefined());
}
});

runtime_->global().setProperty(
*runtime_,
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/cpp/headers/NativeProxy.h
Expand Up @@ -131,6 +131,8 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
jni::alias_ref<LayoutAnimations::javaobject> layoutAnimations);
static void registerNatives();

~NativeProxy();

private:
friend HybridBase;
jni::global_ref<NativeProxy::javaobject> javaPart_;
Expand Down

0 comments on commit 68f0b17

Please sign in to comment.