diff --git a/data_share/frameworks/js/napi/dataShare/include/napi_datashare_helper.h b/data_share/frameworks/js/napi/dataShare/include/napi_datashare_helper.h index b2f195cfc..4f9c39565 100644 --- a/data_share/frameworks/js/napi/dataShare/include/napi_datashare_helper.h +++ b/data_share/frameworks/js/napi/dataShare/include/napi_datashare_helper.h @@ -44,22 +44,8 @@ private: static napi_value GetConstructor(napi_env env); static napi_value Initialize(napi_env env, napi_callback_info info); - static napi_value RegisterWrap(napi_env env, napi_callback_info info, DSHelperOnOffCB *onCB); - static napi_value RegisterAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DSHelperOnOffCB *onCB); - static void RegisterExecuteCB(napi_env env, void *data); - static void RegisterCompleteCB(napi_env env, napi_status status, void *data); - - static napi_value UnRegisterWrap(napi_env env, napi_callback_info info, DSHelperOnOffCB *offCB); - static napi_value UnRegisterAsync( - napi_env env, napi_value *args, size_t argc, const size_t argcPromise, DSHelperOnOffCB *offCB); - static void UnRegisterExecuteCB(napi_env env, void *data); - static void UnRegisterCompleteCB(napi_env env, napi_status status, void *data); - static void FindRegisterObs(napi_env env, DSHelperOnOffCB *data); - static void FindRegisterObsByCallBack(napi_env env, DSHelperOnOffCB *data); - std::shared_ptr datashareHelper_ = nullptr; - std::map> observerMap_; + std::map> observerMap_; struct ContextInfo : public AsyncCall::Context { NapiDataShareHelper *proxy = nullptr; diff --git a/data_share/frameworks/js/napi/dataShare/include/napi_datashare_observer.h b/data_share/frameworks/js/napi/dataShare/include/napi_datashare_observer.h index 97c599c57..f10b49909 100644 --- a/data_share/frameworks/js/napi/dataShare/include/napi_datashare_observer.h +++ b/data_share/frameworks/js/napi/dataShare/include/napi_datashare_observer.h @@ -29,10 +29,10 @@ public: NAPIDataShareObserver(napi_env env, napi_value callback); virtual ~NAPIDataShareObserver(); void OnChange() override; + void DeleteReference(); private: struct ObserverWorker { const NAPIDataShareObserver *observer_ = nullptr; - napi_env env_ = nullptr; ObserverWorker(const NAPIDataShareObserver *observerIn) : observer_(observerIn) {} }; diff --git a/data_share/frameworks/js/napi/dataShare/src/napi_datashare_helper.cpp b/data_share/frameworks/js/napi/dataShare/src/napi_datashare_helper.cpp index 79dce3959..45e185b68 100644 --- a/data_share/frameworks/js/napi/dataShare/src/napi_datashare_helper.cpp +++ b/data_share/frameworks/js/napi/dataShare/src/napi_datashare_helper.cpp @@ -162,7 +162,17 @@ napi_value NapiDataShareHelper::Initialize(napi_env env, napi_callback_info info g_dataShareHelperList.emplace_back(proxy->datashareHelper_); auto finalize = [](napi_env env, void * data, void * hint) { NapiDataShareHelper *proxy = reinterpret_cast(data); - delete proxy; + if (proxy != nullptr) { + auto it = proxy->observerMap_.begin(); + while (it != proxy->observerMap_.end()) { + if (proxy->datashareHelper_ != nullptr) { + proxy->datashareHelper_->UnregisterObserver(Uri(it->first), it->second); + } + it->second->DeleteReference(); + } + proxy->observerMap_.clear(); + delete proxy; + } }; if (napi_wrap(env, self, proxy, finalize, nullptr, nullptr) != napi_ok) { finalize(env, proxy, nullptr); @@ -684,6 +694,7 @@ napi_value NapiDataShareHelper::Napi_On(napi_env env, napi_callback_info info) auto obs = proxy->observerMap_.find(uri); if (obs != proxy->observerMap_.end()) { proxy->datashareHelper_->UnregisterObserver(Uri(uri), obs->second); + obs->second->DeleteReference(); proxy->observerMap_.erase(uri); } proxy->datashareHelper_->RegisterObserver(Uri(uri), observer); @@ -730,6 +741,7 @@ napi_value NapiDataShareHelper::Napi_Off(napi_env env, napi_callback_info info) auto obs = proxy->observerMap_.find(uri); if (obs != proxy->observerMap_.end()) { proxy->datashareHelper_->UnregisterObserver(Uri(uri), obs->second); + obs->second->DeleteReference(); proxy->observerMap_.erase(uri); } else { LOG_DEBUG("this uri hasn't been registered"); diff --git a/data_share/frameworks/js/napi/dataShare/src/napi_datashare_observer.cpp b/data_share/frameworks/js/napi/dataShare/src/napi_datashare_observer.cpp index c27017899..5c15fcc3a 100644 --- a/data_share/frameworks/js/napi/dataShare/src/napi_datashare_observer.cpp +++ b/data_share/frameworks/js/napi/dataShare/src/napi_datashare_observer.cpp @@ -25,16 +25,15 @@ NAPIDataShareObserver::NAPIDataShareObserver(napi_env env, napi_value callback) napi_get_uv_event_loop(env, &loop_); } -NAPIDataShareObserver::~NAPIDataShareObserver() -{ - if (ref_ != nullptr) { - napi_delete_reference(env_, ref_); - } -} +NAPIDataShareObserver::~NAPIDataShareObserver() {} void NAPIDataShareObserver::OnChange() { LOG_DEBUG("OnChange called"); + if (ref_ == nullptr) { + LOG_ERROR("ref_ == nullptr"); + return; + } ObserverWorker *observerWorker = new (std::nothrow)ObserverWorker(this); if (observerWorker == nullptr) { LOG_ERROR("Failed to create observerWorker"); @@ -51,6 +50,10 @@ void NAPIDataShareObserver::OnChange() [](uv_work_t *work, int status) { LOG_DEBUG("uv_queue_work start"); std::shared_ptr innerWorker(reinterpret_cast(work->data)); + if (innerWorker->observer_->ref_ == nullptr) { + LOG_ERROR("innerWorker->observer_->ref_ is nullptr"); + return; + } napi_value callback = nullptr; napi_value args[2] = {0}; napi_value global = nullptr; @@ -70,5 +73,13 @@ void NAPIDataShareObserver::OnChange() delete work; } } + +void NAPIDataShareObserver::DeleteReference() +{ + if (ref_ != nullptr) { + napi_delete_reference(env_, ref_); + ref_ = nullptr; + } +} } // namespace DataShare } // namespace OHOS