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_param_collect.cpp b/services/samgr/native/source/collect/device_param_collect.cpp index 1a89f822e65fb2ef860f27394a7c68852951d51c..5553141930553a7bddec1fa3786bae2485aabebb 100644 --- a/services/samgr/native/source/collect/device_param_collect.cpp +++ b/services/samgr/native/source/collect/device_param_collect.cpp @@ -138,8 +138,12 @@ 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 null"); + } + share->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..682225af43031555582b156838aea73c5954b01e 100644 --- a/services/samgr/native/source/collect/device_switch_collect.cpp +++ b/services/samgr/native/source/collect/device_switch_collect.cpp @@ -131,8 +131,12 @@ void CesStateListener::OnAddSystemAbility(int32_t systemAbilityId, const std::st HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr"); return; } - auto task = [this] () { - auto deviceSwitchCollect = deviceSwitchCollect_.promote(); + auto share = shared_from_this(); + auto task = [share] () { + if (!share) { + HILOGE("CesStateListener is null"); + } + auto deviceSwitchCollect = share->deviceSwitchCollect_.promote(); if (deviceSwitchCollect == nullptr) { HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr"); return; 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..8050860b72be7f65fce99ace317147d5b7aa379b 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 @@ -81,7 +81,6 @@ void DeviceStatusCollectManagerTest::PostTask( std::lock_guard autoLock(caseDoneLock_); isCaseDone = true; caseDoneCondition_.notify_one(); - }; if (collectHandler != nullptr) { collectHandler->PostTask(caseDoneNotifyTask); }