diff --git a/services/samgr/native/include/collect/device_networking_collect.h b/services/samgr/native/include/collect/device_networking_collect.h index 3538ed96ff19ca10edbaddbbb61540b6bb3a1d3b..3836e4ec8f4c44faceb0397c7921a88db3cfc5cb 100644 --- a/services/samgr/native/include/collect/device_networking_collect.h +++ b/services/samgr/native/include/collect/device_networking_collect.h @@ -74,7 +74,7 @@ private: bool ReportMissedEvents(); }; -class WorkHandler { +class WorkHandler : public std::enable_shared_from_this { public: WorkHandler(const sptr& collect) : collect_(collect) { diff --git a/services/samgr/native/include/collect/device_param_collect.h b/services/samgr/native/include/collect/device_param_collect.h index 3441f4df6748537070c3a93198ccd3b484da4001..76563f1fe989006e982ae50f3781cead2065c553 100644 --- a/services/samgr/native/include/collect/device_param_collect.h +++ b/services/samgr/native/include/collect/device_param_collect.h @@ -39,7 +39,8 @@ private: std::set params_; }; -class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub { +class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub, + public std::enable_shared_from_this { public: void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 97ad65b0f3c1f962189b8569faef50171ee29235..e2ef08c94bcf76e3cd50ca19ac63cbe608545bfa 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,7 +293,13 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto task = [this] {this->ProcessEvent(INIT_EVENT);}; + auto workHandler = shared_from_this(); + auto task = [workHandler] { + if (workHandler == nullptr) { + return; + } + workHandler->ProcessEvent(INIT_EVENT); + }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); return; @@ -308,7 +314,14 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto workHandler = shared_from_this(); + auto task = [workHandler, eventId] { + if (workHandler == nullptr) { + HILOGE("WorkHandler_eventId is null!"); + return; + } + workHandler->ProcessEvent(eventId); + }; return handler_->PostTask(task); } @@ -318,7 +331,14 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto workHandler = shared_from_this(); + auto task = [workHandler, eventId] { + if (workHandler == nullptr) { + HILOGE("workHandler_eventId_delayTime is null!"); + return; + } + workHandler->ProcessEvent(eventId); + }; return handler_->PostTask(task, delayTime); } } // namespace OHOS diff --git a/services/samgr/native/source/collect/device_param_collect.cpp b/services/samgr/native/source/collect/device_param_collect.cpp index 1a89f822e65fb2ef860f27394a7c68852951d51c..642f56bc4f64f84d2af4c6f0acd7dabae359b06e 100644 --- a/services/samgr/native/source/collect/device_param_collect.cpp +++ b/services/samgr/native/source/collect/device_param_collect.cpp @@ -138,8 +138,13 @@ void SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, cons HILOGE("DeviceParamCollect is nullptr"); return; } - auto task = [this] () { - deviceParamCollect_->WatchParameters(); + auto share = shared_from_this(); + auto task = [share] () { + if (!share) { + HILOGE("SystemAbilityStatusChange is nullptr"); + return; + } + share->deviceParamCollect_->WatchParameters(); }; deviceParamCollect_->PostDelayTask(task, 0); break;