Skip to content

Commit 19e41c6

Browse files
committed
runtime: While the runtime is terminating, it's possible that it calls finalizers and tries access the Runtime. We need to ensure that an exception is not thrown at this point safely freeing data any data that is held.
1 parent f92e943 commit 19e41c6

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

test-app/runtime/src/main/cpp/runtime/Runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ Runtime::Runtime(JNIEnv *jEnv, jobject runtime, int id)
101101
Runtime *Runtime::GetRuntime(int runtimeId) {
102102
auto runtime = id_to_runtime_cache.Get(runtimeId);
103103

104-
105104
if (runtime == nullptr) {
106105
stringstream ss;
107106
ss << "Cannot find runtime for id:" << runtimeId;
108107
throw NativeScriptException(ss.str());
109108
}
109+
110110
return runtime;
111111
}
112112

@@ -382,10 +382,10 @@ void Runtime::DestroyRuntime() {
382382
tns::GlobalHelpers::onDisposeEnv(env);
383383
napi_close_handle_scope(env, this->global_scope);
384384
delete this->m_objectManager;
385-
js_free_napi_env(env);
386385
Runtime::thread_id_to_rt_cache.Remove(this->my_thread_id);
387386
id_to_runtime_cache.Remove(m_id);
388387
env_to_runtime_cache.erase(env);
388+
js_free_napi_env(env);
389389

390390
#ifndef __V8__
391391
js_free_runtime(rt);

test-app/runtime/src/main/cpp/runtime/objectmanager/ObjectManager.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void ObjectManager::JSObjectProxyFinalizerCallback(napi_env env, void *finalizeD
466466
auto state = reinterpret_cast<JSInstanceInfo *>(finalizeData);
467467
#endif
468468

469-
auto rt = Runtime::GetRuntime(env);
469+
auto rt = Runtime::GetRuntimeUnchecked(env);
470470
if (rt && !rt->is_destroying) {
471471

472472
auto objManager = rt->GetObjectManager();
@@ -528,8 +528,8 @@ napi_value ObjectManager::JSObjectConstructorCallback(napi_env env, napi_callbac
528528
}
529529

530530
void ObjectManager::ReleaseObjectNow(napi_env env, int javaObjectId) {
531-
auto rt = Runtime::GetRuntime(env);
532-
if (rt->is_destroying) return;
531+
auto rt = Runtime::GetRuntimeUnchecked(env);
532+
if (!rt || rt->is_destroying) return;
533533
ObjectManager *objMgr = rt->GetObjectManager();
534534

535535
auto itFound = objMgr->m_weakObjectIds.find(javaObjectId);
@@ -588,12 +588,18 @@ void ObjectManager::OnGarbageCollected(JNIEnv *jEnv, jintArray object_ids) {
588588
jsize length = jenv.GetArrayLength(object_ids);
589589
int *cppArray = jenv.GetIntArrayElements(object_ids, nullptr);
590590
for (jsize i = 0; i < length; i++) {
591+
auto rt = Runtime::GetRuntimeUnchecked(m_env);
592+
if (rt && rt->is_destroying) return;
591593
int javaObjectId = cppArray[i];
592594
auto itFound = this->m_idToObject.find(javaObjectId);
593595
if (itFound != this->m_idToObject.end()) {
594596
napi_delete_reference(m_env, itFound->second);
595597
this->m_idToObject.erase(javaObjectId);
596-
Runtime::GetRuntime(m_env)->js_method_cache->cleanupObject(javaObjectId);
598+
599+
if (rt && !rt->is_destroying) {
600+
rt->js_method_cache->cleanupObject(javaObjectId);
601+
}
602+
597603
DEBUG_WRITE("JS Object released for object id: %d", javaObjectId);
598604
// auto found = this->m_idToProxy.find(javaObjectId);
599605
// if (found != this->m_idToProxy.end()) {

0 commit comments

Comments
 (0)