From 39a3b43d334a20df28d9bbc27c4af725d5f946ae Mon Sep 17 00:00:00 2001 From: ShiJie Date: Fri, 18 Mar 2022 23:14:27 +0800 Subject: [PATCH] fix: mode callback crash Signed-off-by: ShiJie Change-Id: I465318d08a4aeeb3bdde32d253ccae0c2564c508 --- services/native/include/power_mode_module.h | 2 +- services/native/src/power_mode_module.cpp | 13 ++++++++++--- .../native/test/unittest/src/running_lock_test.cpp | 4 ---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/services/native/include/power_mode_module.h b/services/native/include/power_mode_module.h index ba859560..fd0931ef 100644 --- a/services/native/include/power_mode_module.h +++ b/services/native/include/power_mode_module.h @@ -71,7 +71,7 @@ private: void Prepare(); void PublishPowerModeEvent(); - CallbackManager callbackMgr_; + sptr callbackMgr_; void UpdateModepolicy(); void RunAction(); void SetDisplayOffTime(); diff --git a/services/native/src/power_mode_module.cpp b/services/native/src/power_mode_module.cpp index a1f684b2..695a0a67 100644 --- a/services/native/src/power_mode_module.cpp +++ b/services/native/src/power_mode_module.cpp @@ -38,6 +38,7 @@ PowerModeModule::PowerModeModule() : mode_(NORMAL_MODE), lastMode_(LAST_MODE_FLAG), started_(false) { POWER_HILOGI(MODULE_SERVICE, "PowerModeModule create"); + callbackMgr_ = new CallbackManager(); auto policy = DelayedSingleton::GetInstance(); policy->AddAction(PowerModePolicy::ServiceType::DISPLAY_OFFTIME, std::bind(&PowerModeModule::SetDisplayOffTime, this)); @@ -110,18 +111,24 @@ void PowerModeModule::UpdateModepolicy() void PowerModeModule::AddPowerModeCallback(const sptr& callback) { - callbackMgr_.AddCallback(callback); + if (callbackMgr_) { + callbackMgr_->AddCallback(callback); + } } void PowerModeModule::DelPowerModeCallback(const sptr& callback) { - callbackMgr_.RemoveCallback(callback); + if (callbackMgr_) { + callbackMgr_->RemoveCallback(callback); + } } void PowerModeModule::Prepare() { PublishPowerModeEvent(); - callbackMgr_.WaitingCallback(); + if (callbackMgr_) { + callbackMgr_->WaitingCallback(); + } } void PowerModeModule::CallbackManager::AddCallback(const sptr& callback) diff --git a/services/native/test/unittest/src/running_lock_test.cpp b/services/native/test/unittest/src/running_lock_test.cpp index 0f9a2cb0..bce6de1b 100644 --- a/services/native/test/unittest/src/running_lock_test.cpp +++ b/services/native/test/unittest/src/running_lock_test.cpp @@ -29,10 +29,6 @@ std::shared_ptr RunningLockTest::runningLockMgr_ = nullptr; void RunningLockTest::SetUpTestCase(void) { - pmsTest_ = DelayedSpSingleton::GetInstance(); - runningLockMgr_ = std::make_shared(pmsTest_); - ASSERT_TRUE(runningLockMgr_ != nullptr); - ASSERT_TRUE(runningLockMgr_->Init()); } namespace { -- Gitee