diff --git a/helper/object_helper.h b/helper/object_helper.h index f754863bf4a6414104c47718d9363c5a40fb890f..35e3da15b994d4a355177ab866ce4576bf9cd7db 100644 --- a/helper/object_helper.h +++ b/helper/object_helper.h @@ -43,6 +43,7 @@ public: delete[] value; } else { delete value; + value = nullptr; } } }; @@ -60,6 +61,7 @@ public: delete[] data_; } else { delete data_; + data_ = nullptr; } } diff --git a/plugin/timer.cpp b/plugin/timer.cpp index 4cd59adbd8bed6f97299008c00d56ead877b1656..08e413614767c8942582fab8620998ee1b4047ec 100644 --- a/plugin/timer.cpp +++ b/plugin/timer.cpp @@ -52,11 +52,13 @@ bool Timer::RegisterTime(napi_env env) napi_value Timer::SetTimeout(napi_env env, napi_callback_info cbinfo) { + HILOG_INFO("worker:: timer SetTimeout"); return Timer::SetTimeoutInner(env, cbinfo, false); } napi_value Timer::SetInterval(napi_env env, napi_callback_info cbinfo) { + HILOG_INFO("worker:: timer SetInterval"); return Timer::SetTimeoutInner(env, cbinfo, true); } @@ -79,13 +81,16 @@ napi_value Timer::ClearTimer(napi_env env, napi_callback_info cbinfo) HILOG_WARN("handler should be number"); return nullptr; } - auto iter = timerTable.find(tId); - if (iter == timerTable.end()) { - HILOG_INFO("handler not in table"); - return nullptr; + { + std::lock_guard lock(timeLock); + auto iter = timerTable.find(tId); + if (iter == timerTable.end()) { + HILOG_INFO("handler not in table"); + return nullptr; + } + TimerCallbackInfo* callbackInfo = iter->second; + timerTable.erase(tId); } - TimerCallbackInfo* callbackInfo = iter->second; - timerTable.erase(tId); Helper::CloseHelp::DeletePointer(callbackInfo, false); return Helper::NapiHelper::GetUndefinedValue(env); } @@ -111,6 +116,7 @@ void Timer::TimerCallback(uv_timer_t* handle) return; } if (!callbackInfo->repeat_) { + std::lock_guard lock(timeLock); timerTable.erase(callbackInfo->tId_); Helper::CloseHelp::DeletePointer(callbackInfo, false); } else { @@ -158,13 +164,13 @@ napi_value Timer::SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool { std::lock_guard lock(timeLock); tId = timeCallbackId++; + // 4. generate time callback info + napi_ref callbackRef = Helper::NapiHelper::CreateReference(env, argv[0], 1); + TimerCallbackInfo* callbackInfo = + new TimerCallbackInfo(env, tId, timeout, callbackRef, repeat, callbackArgc, callbackArgv); + // 5. push callback info into timerTable + timerTable[tId] = callbackInfo; } - // 4. generate time callback info - napi_ref callbackRef = Helper::NapiHelper::CreateReference(env, argv[0], 1); - TimerCallbackInfo* callbackInfo = - new TimerCallbackInfo(env, tId, timeout, callbackRef, repeat, callbackArgc, callbackArgv); - // 5. push callback info into timerTable - timerTable[tId] = callbackInfo; // 6. start timer uv_timer_start(&callbackInfo->timeReq_, TimerCallback, timeout, timeout > 0 ? timeout : 1); @@ -173,6 +179,7 @@ napi_value Timer::SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool void Timer::ClearEnvironmentTimer(napi_env env) { + std::lock_guard lock(timeLock); auto iter = timerTable.begin(); while (iter != timerTable.end()) { TimerCallbackInfo* callbackInfo = iter->second; diff --git a/plugin/timer.h b/plugin/timer.h index 08a2cb56665bdeed83d68e0a42c51e7dadb0ff12..31a6f6cc4ad70edf6ff070f117cedba035b7bb5c 100644 --- a/plugin/timer.h +++ b/plugin/timer.h @@ -17,6 +17,7 @@ #define JS_WORKER_MODULE_PLUGIN_TIMER_H_ #include +#include #include #include "base/compileruntime/js_worker_module/helper/napi_helper.h" diff --git a/worker/worker.h b/worker/worker.h index df288468ed53ec28cbc3f59224860395447e025c..41715ce03fbea2220a884b324eb344431262bf10 100644 --- a/worker/worker.h +++ b/worker/worker.h @@ -369,6 +369,7 @@ public: uv_run(loop, UV_RUN_DEFAULT); } else { HILOG_ERROR("worker:: Worker loop is nullptr when start worker loop"); + return; } }