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..0ddf8cecd2faabc5b9c2864ae35298f5a5a7dc23 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/include/collect/device_switch_collect.h b/services/samgr/native/include/collect/device_switch_collect.h index 5798d549753b18c7031660ec76edddf8361e71e7..c635ab47e9be3a665a950e0181ffd79ef0946be3 100644 --- a/services/samgr/native/include/collect/device_switch_collect.h +++ b/services/samgr/native/include/collect/device_switch_collect.h @@ -44,7 +44,8 @@ private: wptr deviceSwitchCollect_; }; -class CesStateListener : public SystemAbilityStatusChangeStub { +class CesStateListener : public SystemAbilityStatusChangeStub, +public std::enable_shared_from_this { public: CesStateListener(const sptr& deviceSwitchCollect) : deviceSwitchCollect_(deviceSwitchCollect) {}; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 97ad65b0f3c1f962189b8569faef50171ee29235..a49a592034dd6184497f133b2df6e4798b5c590c 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,7 +293,15 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto task = [this] {this->ProcessEvent(INIT_EVENT);}; + auto weak = weak_from_this(); + auto task = [weak] { + auto strong = weak.lock(); + if (!strong) { + HILOGE("WorkHandler ProcessEvent is null!"); + return; + } + strong->ProcessEvent(INIT_EVENT); + }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); return; @@ -308,7 +316,15 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto weak = weak_from_this(); + auto task = [weak, eventId] { + auto strong = weak.lock(); + if (!strong) { + HILOGE("WorkHandler SendEvent is null!"); + return; + }; + strong->ProcessEvent(eventId); + }; return handler_->PostTask(task); } @@ -318,7 +334,15 @@ 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 weak = weak_from_this(); + auto task = [weak, eventId] { + auto strong = weak.lock(); + if (!strong) { + HILOGE("WorkHandler SendEvent delay is null!"); + return; + }; + strong->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..fbe3bc59e1b75cebeda0d3a5711d8a11cb565da5 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 weak = weak_from_this(); + auto task = [weak] () { + auto strong = weak.lock(); + if (!strong) { + HILOGE("SystemAbilityStatusChange is null"); + } + strong->deviceParamCollect_->WatchParameters(); }; deviceParamCollect_->PostDelayTask(task, 0); break; diff --git a/services/samgr/native/source/collect/device_switch_collect.cpp b/services/samgr/native/source/collect/device_switch_collect.cpp index 69909ed6a979ecd1757553f4fddd2504e1e9fb77..7cbeb7ce522f9b9e28917e5cd620ee3db3b16707 100644 --- a/services/samgr/native/source/collect/device_switch_collect.cpp +++ b/services/samgr/native/source/collect/device_switch_collect.cpp @@ -131,8 +131,13 @@ void CesStateListener::OnAddSystemAbility(int32_t systemAbilityId, const std::st HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr"); return; } - auto task = [this] () { - auto deviceSwitchCollect = deviceSwitchCollect_.promote(); + auto weak = weak_from_this(); + auto task = [weak] () { + auto strong = weak.lock(); + if (!strong) { + HILOGE("CesStateListener is null"); + } + auto deviceSwitchCollect = strong->deviceSwitchCollect_.promote(); if (deviceSwitchCollect == nullptr) { HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr"); return; diff --git a/services/samgr/native/test/unittest/include/device_status_collect_manager_test.h b/services/samgr/native/test/unittest/include/device_status_collect_manager_test.h index b73f6b8e753e9a35776c14cbc4516208e23117fa..9807c41a5610231a48689cfaa770b81f7d2f5aed 100644 --- a/services/samgr/native/test/unittest/include/device_status_collect_manager_test.h +++ b/services/samgr/native/test/unittest/include/device_status_collect_manager_test.h @@ -24,7 +24,8 @@ #include "icollect_plugin.h" namespace OHOS { -class DeviceStatusCollectManagerTest : public testing::Test { +class DeviceStatusCollectManagerTest : public testing::Test, +public std::enable_shared_from_this { public: static void SetUpTestCase(); static void TearDownTestCase(); diff --git a/services/samgr/native/test/unittest/src/device_status_collect_manager_test.cpp b/services/samgr/native/test/unittest/src/device_status_collect_manager_test.cpp index c24703ec4710ec36e0978385defda9c8a0701871..bf6e771afb5b18683f48726913f7f4fd21a3f99e 100644 --- a/services/samgr/native/test/unittest/src/device_status_collect_manager_test.cpp +++ b/services/samgr/native/test/unittest/src/device_status_collect_manager_test.cpp @@ -77,10 +77,16 @@ void DeviceStatusCollectManagerTest::PostTask( std::shared_ptr& collectHandler) { isCaseDone = false; - auto caseDoneNotifyTask = [this]() { - std::lock_guard autoLock(caseDoneLock_); - isCaseDone = true; - caseDoneCondition_.notify_one(); + auto weak = weak_from_this(); + auto caseDoneNotifyTask = [weak]() { + auto strong = weak.lock(); + std::lock_guard autoLock(strong->caseDoneLock_); + if (!strong) { + DTEST_LOG << "DeviceStatusCollectManagerTest is null!" << std::endl; + return; + }; + strong->isCaseDone = true; + strong->caseDoneCondition_.notify_one(); }; if (collectHandler != nullptr) { collectHandler->PostTask(caseDoneNotifyTask);