From 88bc8b22ec735da8e96143107a9460be4b0e685b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Fri, 15 Nov 2024 17:06:24 +0800 Subject: [PATCH 1/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../collect/device_networking_collect.cpp | 27 ++++++++++++++++--- .../device_status_collect_manager_test.cpp | 3 ++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 97ad65b0..0a3aea4c 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,7 +293,14 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto task = [this] {this->ProcessEvent(INIT_EVENT);}; + auto share = share_from_this(); + auto task = [share] { + if (!share){ + HILOGE("WorkHandler ProcessEvent is null!"); + return; + } + share->ProcessEvent(INIT_EVENT); + }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); return; @@ -308,7 +315,14 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto share = share_from_this(); + auto task = [share, eventId] { + if (!share){ + HILOGE("WorkHandler SendEvent is null!"); + return; + } + share->ProcessEvent(eventId); + }; return handler_->PostTask(task); } @@ -318,7 +332,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 share = share_from_this(); + auto task = [share, eventId] { + if (!share){ + HILOGE("WorkHandler SendEvent delay is null!"); + return; + } + share->ProcessEvent(eventId); + }; return handler_->PostTask(task, delayTime); } } // namespace OHOS 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 c24703ec..8662181e 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,7 +77,8 @@ void DeviceStatusCollectManagerTest::PostTask( std::shared_ptr& collectHandler) { isCaseDone = false; - auto caseDoneNotifyTask = [this]() { + auto share = share_from_this(); + auto caseDoneNotifyTask = [share]() { std::lock_guard autoLock(caseDoneLock_); isCaseDone = true; caseDoneCondition_.notify_one(); -- Gitee From 613e68f5c2a646e8a8e5f0bb1daf756f4652a0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Tue, 19 Nov 2024 17:28:56 +0800 Subject: [PATCH 2/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../collect/device_networking_collect.h | 2 +- .../collect/device_networking_collect.cpp | 24 +++++++++---------- .../device_status_collect_manager_test.h | 2 +- .../device_status_collect_manager_test.cpp | 8 +++++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/services/samgr/native/include/collect/device_networking_collect.h b/services/samgr/native/include/collect/device_networking_collect.h index 3538ed96..75d055d0 100644 --- a/services/samgr/native/include/collect/device_networking_collect.h +++ b/services/samgr/native/include/collect/device_networking_collect.h @@ -41,7 +41,7 @@ class DeviceStateCallback : public DistributedHardware::DeviceStateCallback { std::mutex deviceOnlineLock_; }; -class WorkHandler; +class WorkHandler : public std::enable_shared_from_this; class DeviceInitCallBack : public DistributedHardware::DmInitCallback { public: explicit DeviceInitCallBack(const std::shared_ptr& handler) : handler_(handler) {} diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 0a3aea4c..083ea807 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,13 +293,13 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto share = share_from_this(); - auto task = [share] { - if (!share){ + auto weak = weak_from_this(); + auto task = [weak] { + if (!weak) { HILOGE("WorkHandler ProcessEvent is null!"); return; } - share->ProcessEvent(INIT_EVENT); + weak->ProcessEvent(INIT_EVENT); }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); @@ -315,13 +315,13 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto share = share_from_this(); - auto task = [share, eventId] { - if (!share){ + auto weak = weak_from_this(); + auto task = [weak, eventId] { + if (!weak){ HILOGE("WorkHandler SendEvent is null!"); return; } - share->ProcessEvent(eventId); + weak->ProcessEvent(eventId); }; return handler_->PostTask(task); } @@ -332,13 +332,13 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto share = share_from_this(); - auto task = [share, eventId] { - if (!share){ + auto weak = weak_from_this(); + auto task = [weak, eventId] { + if (!weak){ HILOGE("WorkHandler SendEvent delay is null!"); return; } - share->ProcessEvent(eventId); + weak->ProcessEvent(eventId); }; return handler_->PostTask(task, delayTime); } 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 b73f6b8e..2094ebe6 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,7 @@ #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 8662181e..ffadc3fb 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,9 +77,13 @@ void DeviceStatusCollectManagerTest::PostTask( std::shared_ptr& collectHandler) { isCaseDone = false; - auto share = share_from_this(); - auto caseDoneNotifyTask = [share]() { + auto weak = weak_from_this(); + auto caseDoneNotifyTask = [weak]() { std::lock_guard autoLock(caseDoneLock_); + if (!weak) { + HILOGE("DeviceStatusCollectManagerTest is null!"); + return; + } isCaseDone = true; caseDoneCondition_.notify_one(); }; -- Gitee From 8ae18c42068cec9467195b71eb092b5ffcea33bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Tue, 19 Nov 2024 17:38:05 +0800 Subject: [PATCH 3/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../native/source/collect/device_networking_collect.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 083ea807..67bb5846 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -317,10 +317,10 @@ bool WorkHandler::SendEvent(uint32_t eventId) } auto weak = weak_from_this(); auto task = [weak, eventId] { - if (!weak){ + if (!weak) { HILOGE("WorkHandler SendEvent is null!"); return; - } + }; weak->ProcessEvent(eventId); }; return handler_->PostTask(task); @@ -334,10 +334,10 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) } auto weak = weak_from_this(); auto task = [weak, eventId] { - if (!weak){ + if (!weak) { HILOGE("WorkHandler SendEvent delay is null!"); return; - } + }; weak->ProcessEvent(eventId); }; return handler_->PostTask(task, delayTime); -- Gitee From 1e89214bbe0606d8767d859f499b34f0cda8f6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Wed, 20 Nov 2024 10:07:26 +0800 Subject: [PATCH 4/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../collect/device_networking_collect.h | 4 ++-- .../collect/device_networking_collect.cpp | 24 +++++++++---------- .../device_status_collect_manager_test.h | 3 ++- .../device_status_collect_manager_test.cpp | 8 +++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/services/samgr/native/include/collect/device_networking_collect.h b/services/samgr/native/include/collect/device_networking_collect.h index 75d055d0..3836e4ec 100644 --- a/services/samgr/native/include/collect/device_networking_collect.h +++ b/services/samgr/native/include/collect/device_networking_collect.h @@ -41,7 +41,7 @@ class DeviceStateCallback : public DistributedHardware::DeviceStateCallback { std::mutex deviceOnlineLock_; }; -class WorkHandler : public std::enable_shared_from_this; +class WorkHandler; class DeviceInitCallBack : public DistributedHardware::DmInitCallback { public: explicit DeviceInitCallBack(const std::shared_ptr& handler) : handler_(handler) {} @@ -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/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 67bb5846..2bff4692 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,13 +293,13 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto weak = weak_from_this(); - auto task = [weak] { - if (!weak) { + auto share = shared_from_this(); + auto task = [share] { + if (!share) { HILOGE("WorkHandler ProcessEvent is null!"); return; } - weak->ProcessEvent(INIT_EVENT); + share->ProcessEvent(INIT_EVENT); }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); @@ -315,13 +315,13 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto weak = weak_from_this(); - auto task = [weak, eventId] { - if (!weak) { + auto share = shared_from_this(); + auto task = [share, eventId] { + if (!share) { HILOGE("WorkHandler SendEvent is null!"); return; }; - weak->ProcessEvent(eventId); + share->ProcessEvent(eventId); }; return handler_->PostTask(task); } @@ -332,13 +332,13 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto weak = weak_from_this(); - auto task = [weak, eventId] { - if (!weak) { + auto share = shared_from_this(); + auto task = [share, eventId] { + if (!share) { HILOGE("WorkHandler SendEvent delay is null!"); return; }; - weak->ProcessEvent(eventId); + share->ProcessEvent(eventId); }; return handler_->PostTask(task, delayTime); } 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 2094ebe6..9807c41a 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, public std::enable_shared_from_this { +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 ffadc3fb..51986db3 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,13 +77,13 @@ void DeviceStatusCollectManagerTest::PostTask( std::shared_ptr& collectHandler) { isCaseDone = false; - auto weak = weak_from_this(); - auto caseDoneNotifyTask = [weak]() { + auto share = shared_from_this(); + auto caseDoneNotifyTask = [share]() { std::lock_guard autoLock(caseDoneLock_); - if (!weak) { + if (!share) { HILOGE("DeviceStatusCollectManagerTest is null!"); return; - } + }; isCaseDone = true; caseDoneCondition_.notify_one(); }; -- Gitee From 0dd824e077ac17de39edf5c9e358e523b693ae9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Wed, 20 Nov 2024 14:40:08 +0800 Subject: [PATCH 5/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../unittest/src/device_status_collect_manager_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 51986db3..eb5acc65 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 @@ -79,13 +79,13 @@ void DeviceStatusCollectManagerTest::PostTask( isCaseDone = false; auto share = shared_from_this(); auto caseDoneNotifyTask = [share]() { - std::lock_guard autoLock(caseDoneLock_); + std::lock_guard autoLock(share->caseDoneLock_); if (!share) { - HILOGE("DeviceStatusCollectManagerTest is null!"); + DTEST_LOG << "DeviceStatusCollectManagerTest is null!" << std::endl; return; }; - isCaseDone = true; - caseDoneCondition_.notify_one(); + share->isCaseDone = true; + share->caseDoneCondition_.notify_one(); }; if (collectHandler != nullptr) { collectHandler->PostTask(caseDoneNotifyTask); -- Gitee From d0fa3458bc6845cb5bd3e82db8fe82b2c3a013da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Wed, 20 Nov 2024 15:26:25 +0800 Subject: [PATCH 6/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../samgr/native/include/collect/device_param_collect.h | 3 ++- .../samgr/native/include/collect/device_switch_collect.h | 3 ++- .../samgr/native/source/collect/device_param_collect.cpp | 8 ++++++-- .../samgr/native/source/collect/device_switch_collect.cpp | 8 ++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/services/samgr/native/include/collect/device_param_collect.h b/services/samgr/native/include/collect/device_param_collect.h index 3441f4df..0ddf8cec 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 5798d549..c635ab47 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 1a89f822..55531419 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 69909ed6..682225af 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; -- Gitee From 906f26ce7fffbf5ca7c3d1ee539fcab1b7a17a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Fri, 22 Nov 2024 15:16:24 +0800 Subject: [PATCH 7/7] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../collect/device_networking_collect.cpp | 27 ++++++++++--------- .../source/collect/device_param_collect.cpp | 9 ++++--- .../source/collect/device_switch_collect.cpp | 9 ++++--- .../device_status_collect_manager_test.cpp | 13 ++++----- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 2bff4692..a49a5920 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,13 +293,14 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto share = shared_from_this(); - auto task = [share] { - if (!share) { + auto weak = weak_from_this(); + auto task = [weak] { + auto strong = weak.lock(); + if (!strong) { HILOGE("WorkHandler ProcessEvent is null!"); return; } - share->ProcessEvent(INIT_EVENT); + strong->ProcessEvent(INIT_EVENT); }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); @@ -315,13 +316,14 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto share = shared_from_this(); - auto task = [share, eventId] { - if (!share) { + auto weak = weak_from_this(); + auto task = [weak, eventId] { + auto strong = weak.lock(); + if (!strong) { HILOGE("WorkHandler SendEvent is null!"); return; }; - share->ProcessEvent(eventId); + strong->ProcessEvent(eventId); }; return handler_->PostTask(task); } @@ -332,13 +334,14 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto share = shared_from_this(); - auto task = [share, eventId] { - if (!share) { + auto weak = weak_from_this(); + auto task = [weak, eventId] { + auto strong = weak.lock(); + if (!strong) { HILOGE("WorkHandler SendEvent delay is null!"); return; }; - share->ProcessEvent(eventId); + strong->ProcessEvent(eventId); }; return handler_->PostTask(task, delayTime); } diff --git a/services/samgr/native/source/collect/device_param_collect.cpp b/services/samgr/native/source/collect/device_param_collect.cpp index 55531419..fbe3bc59 100644 --- a/services/samgr/native/source/collect/device_param_collect.cpp +++ b/services/samgr/native/source/collect/device_param_collect.cpp @@ -138,12 +138,13 @@ void SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, cons HILOGE("DeviceParamCollect is nullptr"); return; } - auto share = shared_from_this(); - auto task = [share] () { - if (!share) { + auto weak = weak_from_this(); + auto task = [weak] () { + auto strong = weak.lock(); + if (!strong) { HILOGE("SystemAbilityStatusChange is null"); } - share->deviceParamCollect_->WatchParameters(); + 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 682225af..7cbeb7ce 100644 --- a/services/samgr/native/source/collect/device_switch_collect.cpp +++ b/services/samgr/native/source/collect/device_switch_collect.cpp @@ -131,12 +131,13 @@ void CesStateListener::OnAddSystemAbility(int32_t systemAbilityId, const std::st HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr"); return; } - auto share = shared_from_this(); - auto task = [share] () { - if (!share) { + auto weak = weak_from_this(); + auto task = [weak] () { + auto strong = weak.lock(); + if (!strong) { HILOGE("CesStateListener is null"); } - auto deviceSwitchCollect = share->deviceSwitchCollect_.promote(); + auto deviceSwitchCollect = strong->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 eb5acc65..bf6e771a 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,15 +77,16 @@ void DeviceStatusCollectManagerTest::PostTask( std::shared_ptr& collectHandler) { isCaseDone = false; - auto share = shared_from_this(); - auto caseDoneNotifyTask = [share]() { - std::lock_guard autoLock(share->caseDoneLock_); - if (!share) { + 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; }; - share->isCaseDone = true; - share->caseDoneCondition_.notify_one(); + strong->isCaseDone = true; + strong->caseDoneCondition_.notify_one(); }; if (collectHandler != nullptr) { collectHandler->PostTask(caseDoneNotifyTask); -- Gitee