From fefad3ad8309547ea9dedc3b262dcbf26c758658 Mon Sep 17 00:00:00 2001 From: y00576111 Date: Wed, 2 Mar 2022 20:30:21 +0800 Subject: [PATCH] fixed 2dede29 from https://gitee.com/yaojian16/js_worker_module/pulls/40 actor release bugfix Repair the crash caused by the worker using the nativeengine when the main thread nativeengine has been destructed issue:https://gitee.com/openharmony/js_worker_module/issues/I4W1BS Signed-off-by: y00576111 Change-Id: Iddb9dda2290555533444b65c138212b4fddb93cd --- jsapi/worker/worker.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index ecef654..67cd1b5 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -719,9 +719,13 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) return nullptr; } worker->StartExecuteInThread(env, script); - napi_wrap( - env, thisVar, worker, - [](napi_env env, void* data, void* hint) { + napi_wrap(env, thisVar, worker, [](napi_env env, void* data, void* hint) {}, nullptr, nullptr); + + auto hostEngine = reinterpret_cast(env); + auto thisNativeValue = reinterpret_cast(thisVar); + auto thisReference = hostEngine->CreateReference(thisNativeValue, 1, true, + [](NativeEngine* engine, void* data, void* hint) { + HILOG_ERROR("worker:: thisReference destroy"); Worker* worker = (Worker*)data; { std::lock_guard lock(worker->liveStatusLock_); @@ -740,9 +744,8 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) } worker->TerminateInner(); } - }, - nullptr, nullptr); - napi_create_reference(env, thisVar, 1, &worker->workerWrapper_); + }, worker, nullptr); + worker->workerWrapper_ = reinterpret_cast(thisReference); return thisVar; } -- Gitee