Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSCRuntime destroyed with a dangling API object #3185

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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