Skip to content

Commit 77bc2cb

Browse files
committed
runtime: fix exception thrown during runtime cleanup
1 parent 9062b8c commit 77bc2cb

4 files changed

Lines changed: 17 additions & 18 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void Runtime::Init(JNIEnv *_env, jstring filesPath, jstring nativeLibsDir,
166166
napi_handle_scope handleScope;
167167
napi_open_handle_scope(env, &handleScope);
168168

169-
env_to_runtime_cache.emplace(env, this);
169+
env_to_runtime_cache.Insert(env, this);
170170

171171
napi_value global;
172172
napi_get_global(env, &global);
@@ -341,6 +341,7 @@ ObjectManager *Runtime::GetObjectManager() const {
341341
}
342342

343343
Runtime::~Runtime() {
344+
delete this->m_objectManager;
344345
delete this->m_loopTimer;
345346

346347
#ifdef __V8__
@@ -371,20 +372,21 @@ std::string Runtime::ReadFileText(const std::string &filePath) {
371372
}
372373

373374
void Runtime::DestroyRuntime() {
375+
DEBUG_WRITE_FORCE("%s", "DESTROYING RUNTIME NOW");
374376
is_destroying = true;
375377
MetadataNode::onDisposeEnv(env);
376378
ArgConverter::onDisposeEnv(env);
379+
tns::GlobalHelpers::onDisposeEnv(env);
377380
this->js_method_cache->cleanupCache();
378381
delete this->js_method_cache;
379382
this->m_module.DeInit();
380383
Console::onDisposeEnv(env);
381384
CallbackHandlers::RemoveEnvEntries(env);
382-
tns::GlobalHelpers::onDisposeEnv(env);
385+
this->m_objectManager->OnDisposeEnv();
383386
napi_close_handle_scope(env, this->global_scope);
384-
delete this->m_objectManager;
385387
Runtime::thread_id_to_rt_cache.Remove(this->my_thread_id);
386388
id_to_runtime_cache.Remove(m_id);
387-
env_to_runtime_cache.erase(env);
389+
env_to_runtime_cache.Remove(env);
388390
js_free_napi_env(env);
389391

390392
#ifndef __V8__
@@ -679,7 +681,7 @@ void Runtime::Unlock() {
679681
JavaVM *Runtime::java_vm = nullptr;
680682
jmethodID Runtime::GET_USED_MEMORY_METHOD_ID = nullptr;
681683
tns::ConcurrentMap<int, Runtime *> Runtime::id_to_runtime_cache;
682-
robin_hood::unordered_map<napi_env, Runtime *> Runtime::env_to_runtime_cache;
684+
tns::ConcurrentMap<napi_env, Runtime *> Runtime::env_to_runtime_cache;
683685
bool Runtime::s_mainThreadInitialized = false;
684686
int Runtime::m_androidVersion = Runtime::GetAndroidVersion();
685687
ALooper *Runtime::m_mainLooper = nullptr;

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,16 @@ namespace tns {
3131
static Runtime *GetRuntime(int runtimeId);
3232

3333
inline static Runtime *GetRuntime(napi_env env) {
34-
auto runtime = env_to_runtime_cache.at(env);
34+
auto runtime = env_to_runtime_cache.Get(env);
35+
if (runtime) return runtime;
3536

36-
if (runtime == nullptr) {
37-
std::stringstream ss;
38-
ss << "Cannot find runtime for napi_env: " << env;
39-
throw NativeScriptException(ss.str());
40-
}
41-
42-
return runtime;
37+
std::stringstream ss;
38+
ss << "Cannot find runtime for napi_env: " << env;
39+
throw NativeScriptException(ss.str());
4340
}
4441

4542
inline static Runtime *GetRuntimeUnchecked(napi_env env) {
46-
auto runtime = env_to_runtime_cache.at(env);
47-
return runtime;
43+
return env_to_runtime_cache.Get(env);
4844
}
4945

5046
static void Init(JavaVM *vm);
@@ -169,7 +165,7 @@ namespace tns {
169165

170166
static tns::ConcurrentMap<int, Runtime *> id_to_runtime_cache;
171167

172-
static robin_hood::unordered_map<napi_env, Runtime *> env_to_runtime_cache;
168+
static tns::ConcurrentMap<napi_env, Runtime *> env_to_runtime_cache;
173169

174170
static tns::ConcurrentMap<std::thread::id, Runtime *> thread_id_to_rt_cache;
175171

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ void ObjectManager::Init(napi_env env) {
6464
m_jsObjectCtor = napi_util::make_ref(env, jsObjectCtor, 1);
6565
}
6666

67-
ObjectManager::~ObjectManager() {
67+
68+
void ObjectManager::OnDisposeEnv() {
6869
JEnv jEnv;
6970
if (this->m_jsObjectCtor) napi_delete_reference(m_env, this->m_jsObjectCtor);
7071
if (this->m_jsObjectProxyCreator) napi_delete_reference(m_env, this->m_jsObjectProxyCreator);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace tns {
2020
public:
2121
ObjectManager(jobject javaRuntimeObject);
2222

23-
~ObjectManager();
23+
void OnDisposeEnv();
2424

2525
void Init(napi_env env);
2626

0 commit comments

Comments
 (0)