From c99e56064e157c7e2689c2acea17f68b7f04bd45 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 3 Aug 2021 21:53:29 +0800 Subject: [PATCH] fixed async this reference bugs Signed-off-by: Sven Wang --- frameworks/jskitsimpl/distributeddata/include/async_call.h | 1 + frameworks/jskitsimpl/distributeddata/src/async_call.cpp | 6 ++++++ frameworks/jskitsimpl/distributeddata/src/js_util.cpp | 1 + 3 files changed, 8 insertions(+) diff --git a/frameworks/jskitsimpl/distributeddata/include/async_call.h b/frameworks/jskitsimpl/distributeddata/include/async_call.h index e491ea8c7..892ff9c3b 100644 --- a/frameworks/jskitsimpl/distributeddata/include/async_call.h +++ b/frameworks/jskitsimpl/distributeddata/include/async_call.h @@ -91,6 +91,7 @@ private: struct AsyncContext { std::shared_ptr ctx = nullptr; napi_ref callback = nullptr; + napi_ref self = nullptr; napi_deferred defer = nullptr; napi_async_work work = nullptr; }; diff --git a/frameworks/jskitsimpl/distributeddata/src/async_call.cpp b/frameworks/jskitsimpl/distributeddata/src/async_call.cpp index 4682e1dea..e9add7590 100644 --- a/frameworks/jskitsimpl/distributeddata/src/async_call.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/async_call.cpp @@ -39,6 +39,7 @@ AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptrctx = std::move(context); NAPI_CALL_RETURN_VOID(env, (*context_->ctx)(env, argc, argv, self)); + napi_create_reference(env, self, 1, &context_->self); } AsyncCall::~AsyncCall() @@ -55,6 +56,10 @@ napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec) if (context_ == nullptr) { return nullptr; } + if (context_->ctx == nullptr) { + ZLOGD("context_ ctx is null"); + return nullptr; + } ZLOGD("async call exec"); context_->ctx->exec_ = std::move(exec); napi_value promise = nullptr; @@ -139,6 +144,7 @@ void AsyncCall::DeleteContext(napi_env env, AsyncContext *context) { if (env != nullptr) { napi_delete_reference(env, context->callback); + napi_delete_reference(env, context->self); napi_delete_async_work(env, context->work); } delete context; diff --git a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index 179cc645a..cae0f1bfe 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp @@ -39,6 +39,7 @@ DistributedKv::Options JSUtil::Convert2Options(napi_env env, napi_value jsOption napi_get_value_bool(env, value, &options.backup); } value = nullptr; + options.autoSync = false; napi_get_named_property(env, jsOptions, "autoSync", &value); if (value != nullptr) { napi_get_value_bool(env, value, &options.autoSync); -- Gitee