From c21f246895707c81ca2ada44cb79f92887347344 Mon Sep 17 00:00:00 2001 From: zhongjianfei Date: Sat, 22 Jan 2022 17:10:17 +0800 Subject: [PATCH] zhongjianfei@huawei.com Signed-off-by: zhongjianfei Change-Id: I6df48884c71ee12dac0f332fe7c72923e254d1ef --- .../ability/native/src/ability_lifecycle.cpp | 6 +- .../ability/native/src/ability_thread.cpp | 131 ++++++++++++------ .../ability/native/src/ability_window.cpp | 4 +- .../appkit/native/app/src/main_thread.cpp | 123 +++++++++++++--- .../abilitymgr/src/ability_stack_manager.cpp | 2 +- .../src/kernal_system_app_manager.cpp | 18 ++- .../src/lock_screen_event_subscriber.cpp | 2 +- .../abilitymgr/src/mission_list_manager.cpp | 4 + .../src/mission_listener_controller.cpp | 42 ++++-- .../abilitymgr/src/pending_want_manager.cpp | 2 +- 10 files changed, 257 insertions(+), 77 deletions(-) diff --git a/frameworks/kits/ability/native/src/ability_lifecycle.cpp b/frameworks/kits/ability/native/src/ability_lifecycle.cpp index f8d39c93754..28c7ee112f8 100644 --- a/frameworks/kits/ability/native/src/ability_lifecycle.cpp +++ b/frameworks/kits/ability/native/src/ability_lifecycle.cpp @@ -61,8 +61,7 @@ void LifeCycle::AddObserver(const std::shared_ptr &observer) */ void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event, const Want &want) { - APP_LOGI("LifeCycle::DispatchLifecycle: called"); - + APP_LOGI("LifeCycle::DispatchLifecycle: event:%{public}d", event); if ((event != LifeCycle::Event::ON_FOREGROUND) && (event != LifeCycle::Event::ON_START)) { APP_LOGE("event value error: event is %{public}d", event); return; @@ -101,8 +100,7 @@ void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event, const Want &wan */ void LifeCycle::DispatchLifecycle(const LifeCycle::Event &event) { - APP_LOGI("LifeCycle::DispatchLifecycle: called"); - + APP_LOGI("LifeCycle::DispatchLifecycle: event:%{public}d", event); if ((event != LifeCycle::Event::ON_ACTIVE) && (event != LifeCycle::Event::ON_BACKGROUND) && (event != LifeCycle::Event::ON_INACTIVE) && (event != LifeCycle::Event::ON_STOP)) { APP_LOGE("event value error: event is %{public}d", event); diff --git a/frameworks/kits/ability/native/src/ability_thread.cpp b/frameworks/kits/ability/native/src/ability_thread.cpp index b161deeeb38..f40b501af67 100644 --- a/frameworks/kits/ability/native/src/ability_thread.cpp +++ b/frameworks/kits/ability/native/src/ability_thread.cpp @@ -667,7 +667,15 @@ void AbilityThread::ScheduleUpdateConfiguration(const Configuration &config) return; } - auto task = [abilitThread = this, config]() { abilitThread->HandleUpdateConfiguration(config); }; + wptr weak = this; + auto task = [weak, config]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleUpdateConfiguration failed."); + return; + } + abilityThread->HandleUpdateConfiguration(config); + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleUpdateConfiguration abilityHandler_ is nullptr"); @@ -716,13 +724,19 @@ void AbilityThread::ScheduleAbilityTransaction(const Want &want, const LifeCycle APP_LOGE("ScheduleAbilityTransaction::failed, token_ nullptr"); return; } - auto task = [abilityThread = this, want, lifeCycleStateInfo]() { - if (abilityThread->isExtension_) { - abilityThread->HandleExtensionTransaction(want, lifeCycleStateInfo); - } else { - abilityThread->HandleAbilityTransaction(want, lifeCycleStateInfo); - } - }; + wptr weak = this; + auto task = [weak, want, lifeCycleStateInfo]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleAbilityTransaction failed."); + return; + } + if (abilityThread->isExtension_) { + abilityThread->HandleExtensionTransaction(want, lifeCycleStateInfo); + } else { + abilityThread->HandleAbilityTransaction(want, lifeCycleStateInfo); + } + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleAbilityTransaction abilityHandler_ == nullptr"); @@ -743,13 +757,19 @@ void AbilityThread::ScheduleAbilityTransaction(const Want &want, const LifeCycle void AbilityThread::ScheduleConnectAbility(const Want &want) { APP_LOGI("AbilityThread::ScheduleConnectAbility begin, isExtension_:%{public}d", isExtension_); - auto task = [abilityThread = this, want]() { - if (abilityThread->isExtension_) { - abilityThread->HandleConnectExtension(want); - } else { - abilityThread->HandleConnectAbility(want); - } - }; + wptr weak = this; + auto task = [weak, want]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleConnectAbility failed."); + return; + } + if (abilityThread->isExtension_) { + abilityThread->HandleConnectExtension(want); + } else { + abilityThread->HandleConnectAbility(want); + } + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleConnectAbility abilityHandler_ == nullptr"); @@ -770,13 +790,19 @@ void AbilityThread::ScheduleConnectAbility(const Want &want) void AbilityThread::ScheduleDisconnectAbility(const Want &want) { APP_LOGI("AbilityThread::ScheduleDisconnectAbility begin, isExtension_:%{public}d", isExtension_); - auto task = [abilityThread = this, want]() { - if (abilityThread->isExtension_) { - abilityThread->HandleDisconnectExtension(want); - } else { - abilityThread->HandleDisconnectAbility(want); - } - }; + wptr weak = this; + auto task = [weak, want]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleDisconnectAbility failed."); + return; + } + if (abilityThread->isExtension_) { + abilityThread->HandleDisconnectExtension(want); + } else { + abilityThread->HandleDisconnectAbility(want); + } + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleDisconnectAbility abilityHandler_ == nullptr"); @@ -805,13 +831,19 @@ void AbilityThread::ScheduleDisconnectAbility(const Want &want) void AbilityThread::ScheduleCommandAbility(const Want &want, bool restart, int startId) { APP_LOGI("AbilityThread::ScheduleCommandAbility begin. startId:%{public}d", startId); - auto task = [abilityThread = this, want, restart, startId]() { - if (abilityThread->isExtension_) { - abilityThread->HandleCommandExtension(want, restart, startId); - } else { - abilityThread->HandleCommandAbility(want, restart, startId); - } - }; + wptr weak = this; + auto task = [weak, want, restart, startId]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleCommandAbility failed."); + return; + } + if (abilityThread->isExtension_) { + abilityThread->HandleCommandExtension(want, restart, startId); + } else { + abilityThread->HandleCommandAbility(want, restart, startId); + } + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleCommandAbility abilityHandler_ == nullptr"); @@ -843,11 +875,16 @@ void AbilityThread::SendResult(int requestCode, int resultCode, const Want &want APP_LOGE("AbilityThread::SendResult abilityImpl_ == nullptr"); return; } - - auto task = [this, requestCode, resultCode, want]() { + wptr weak = this; + auto task = [weak, requestCode, resultCode, want]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr || abilityThread->abilityImpl_ == nullptr) { + APP_LOGE("abilityThread or abilityImpl is nullptr, SendResult failed."); + return; + } if (requestCode != -1) { APP_LOGI("AbilityThread::SendResult before abilityImpl_->SendResult"); - abilityImpl_->SendResult(requestCode, resultCode, want); + abilityThread->abilityImpl_->SendResult(requestCode, resultCode, want); APP_LOGI("AbilityThread::SendResult after abilityImpl_->SendResult"); } }; @@ -862,7 +899,6 @@ void AbilityThread::SendResult(int requestCode, int resultCode, const Want &want APP_LOGE("AbilityThread::SendResult PostTask error"); } APP_LOGI("AbilityThread::SendResult end"); - } /** @@ -1387,13 +1423,18 @@ bool AbilityThread::CheckObsPermission() bool AbilityThread::ScheduleRegisterObserver(const Uri &uri, const sptr &dataObserver) { APP_LOGI("%{public}s called", __func__); - if (!CheckObsPermission()) { APP_LOGE("%{public}s CheckObsPermission() return false", __func__); return false; } - auto task = [abilityThread = this, uri, dataObserver]() { + wptr weak = this; + auto task = [weak, uri, dataObserver]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleRegisterObserver failed."); + return; + } abilityThread->HandleRegisterObserver(uri, dataObserver); }; @@ -1418,13 +1459,18 @@ bool AbilityThread::ScheduleRegisterObserver(const Uri &uri, const sptr &dataObserver) { APP_LOGI("%{public}s called", __func__); - if (!CheckObsPermission()) { APP_LOGE("%{public}s CheckObsPermission() return false", __func__); return false; } - auto task = [abilityThread = this, uri, dataObserver]() { + wptr weak = this; + auto task = [weak, uri, dataObserver]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleUnregisterObserver failed."); + return; + } abilityThread->HandleUnregisterObserver(uri, dataObserver); }; @@ -1448,13 +1494,20 @@ bool AbilityThread::ScheduleUnregisterObserver(const Uri &uri, const sptrHandleNotifyChange(uri); }; + wptr weak = this; + auto task = [weak, uri]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + APP_LOGE("abilityThread is nullptr, ScheduleNotifyChange failed."); + return; + } + abilityThread->HandleNotifyChange(uri); + }; if (abilityHandler_ == nullptr) { APP_LOGE("AbilityThread::ScheduleNotifyChange abilityHandler_ == nullptr"); diff --git a/frameworks/kits/ability/native/src/ability_window.cpp b/frameworks/kits/ability/native/src/ability_window.cpp index c595e1b7227..25c1427e12a 100644 --- a/frameworks/kits/ability/native/src/ability_window.cpp +++ b/frameworks/kits/ability/native/src/ability_window.cpp @@ -195,7 +195,7 @@ void AbilityWindow::OnPostAbilityStop() if (windowScene_) { windowScene_ = nullptr; - APP_LOGI("AbilityWindow::widow:: windowScene_ release end."); + APP_LOGI("AbilityWindow::widow windowScene_ release end."); } isWindowAttached = false; @@ -215,4 +215,4 @@ const sptr AbilityWindow::GetWindow() return windowScene_ ? windowScene_->GetMainWindow() : nullptr; } } // namespace AppExecFwk -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index 23ad30a9702..4b67f313aec 100644 --- a/frameworks/kits/appkit/native/app/src/main_thread.cpp +++ b/frameworks/kits/appkit/native/app/src/main_thread.cpp @@ -257,7 +257,15 @@ void MainThread::ScheduleForegroundApplication() { BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__); APP_LOGI("MainThread::scheduleForegroundApplication called begin"); - auto task = [appThread = this]() { appThread->HandleForegroundApplication(); }; + wptr weak = this; + auto task = [weak]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleForegroundApplication failed."); + return; + } + appThread->HandleForegroundApplication(); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("PostTask task failed"); } @@ -272,8 +280,15 @@ void MainThread::ScheduleForegroundApplication() void MainThread::ScheduleBackgroundApplication() { APP_LOGI("MainThread::scheduleBackgroundApplication called begin"); - - auto task = [appThread = this]() { appThread->HandleBackgroundApplication(); }; + wptr weak = this; + auto task = [weak]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleBackgroundApplication failed."); + return; + } + appThread->HandleBackgroundApplication(); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleBackgroundApplication PostTask task failed"); } @@ -288,8 +303,15 @@ void MainThread::ScheduleBackgroundApplication() void MainThread::ScheduleTerminateApplication() { APP_LOGI("MainThread::scheduleTerminateApplication called begin"); - - auto task = [appThread = this]() { appThread->HandleTerminateApplication(); }; + wptr weak = this; + auto task = [weak]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleTerminateApplication failed."); + return; + } + appThread->HandleTerminateApplication(); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleTerminateApplication PostTask task failed"); } @@ -305,8 +327,15 @@ void MainThread::ScheduleTerminateApplication() void MainThread::ScheduleShrinkMemory(const int level) { APP_LOGI("MainThread::scheduleShrinkMemory level: %{public}d", level); - - auto task = [appThread = this, level]() { appThread->HandleShrinkMemory(level); }; + wptr weak = this; + auto task = [weak, level]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleShrinkMemory failed."); + return; + } + appThread->HandleShrinkMemory(level); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleShrinkMemory PostTask task failed"); } @@ -321,8 +350,15 @@ void MainThread::ScheduleShrinkMemory(const int level) void MainThread::ScheduleProcessSecurityExit() { APP_LOGI("MainThread::ScheduleProcessSecurityExit called start"); - - auto task = [appThread = this]() { appThread->HandleProcessSecurityExit(); }; + wptr weak = this; + auto task = [weak]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleShrinkMemory failed."); + return; + } + appThread->HandleProcessSecurityExit(); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleProcessSecurityExit PostTask task failed"); } @@ -350,8 +386,15 @@ void MainThread::ScheduleLaunchApplication(const AppLaunchData &data) { BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__); APP_LOGI("MainThread::scheduleLaunchApplication start"); - - auto task = [appThread = this, data]() { appThread->HandleLaunchApplication(data); }; + wptr weak = this; + auto task = [weak, data]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleLaunchApplication failed."); + return; + } + appThread->HandleLaunchApplication(data); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleLaunchApplication PostTask task failed"); } @@ -361,7 +404,15 @@ void MainThread::ScheduleLaunchApplication(const AppLaunchData &data) void MainThread::ScheduleAbilityStage(const HapModuleInfo &abilityStage) { APP_LOGI("MainThread::ScheduleAbilityStageInfo start"); - auto task = [appThread = this, abilityStage]() { appThread->HandleAbilityStage(abilityStage);}; + wptr weak = this; + auto task = [weak, abilityStage]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleShrinkMemory failed."); + return; + } + appThread->HandleAbilityStage(abilityStage); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleAbilityStageInfo PostTask task failed"); } @@ -401,7 +452,15 @@ void MainThread::ScheduleLaunchAbility(const AbilityInfo &info, const sptrHandleLaunchAbility(abilityRecord); }; + wptr weak = this; + auto task = [weak, abilityRecord]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleLaunchAbility failed."); + return; + } + appThread->HandleLaunchAbility(abilityRecord); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleLaunchAbility PostTask task failed"); } @@ -418,7 +477,15 @@ void MainThread::ScheduleLaunchAbility(const AbilityInfo &info, const sptr &token) { APP_LOGI("MainThread::scheduleCleanAbility called start."); - auto task = [appThread = this, token]() { appThread->HandleCleanAbility(token); }; + wptr weak = this; + auto task = [weak, token]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleCleanAbility failed."); + return; + } + appThread->HandleCleanAbility(token); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleCleanAbility PostTask task failed"); } @@ -447,7 +514,15 @@ void MainThread::ScheduleProfileChanged(const Profile &profile) void MainThread::ScheduleConfigurationUpdated(const Configuration &config) { APP_LOGI("MainThread::ScheduleConfigurationUpdated called start."); - auto task = [appThread = this, config]() { appThread->HandleConfigurationUpdated(config); }; + wptr weak = this; + auto task = [weak, config]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleConfigurationUpdated failed."); + return; + } + appThread->HandleConfigurationUpdated(config); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleConfigurationUpdated PostTask task failed"); } @@ -1158,7 +1233,13 @@ void MainThread::Init(const std::shared_ptr &runner, const std::sha APP_LOGI("MainThread:Init Start"); mainHandler_ = std::make_shared(runner, this); watchDogHandler_ = std::make_shared(watchDogRunner); - auto task = [appThread = this]() { + wptr weak = this; + auto task = [weak]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("abilityThread is nullptr, SetRunnerStarted failed."); + return; + } APP_LOGI("MainThread:MainHandler Start"); appThread->SetRunnerStarted(true); }; @@ -1460,7 +1541,15 @@ void MainThread::HandleScheduleAcceptWant(const AAFwk::Want &want, const std::st void MainThread::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName) { APP_LOGI("MainThread::ScheduleAcceptWant start"); - auto task = [appThread = this, want, moduleName]() { appThread->HandleScheduleAcceptWant(want, moduleName); }; + wptr weak = this; + auto task = [weak, want, moduleName]() { + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("abilityThread is nullptr, HandleScheduleAcceptWant failed."); + return; + } + appThread->HandleScheduleAcceptWant(want, moduleName); + }; if (!mainHandler_->PostTask(task)) { APP_LOGE("MainThread::ScheduleAcceptWant PostTask task failed"); } diff --git a/services/abilitymgr/src/ability_stack_manager.cpp b/services/abilitymgr/src/ability_stack_manager.cpp index 4ae2b321f4f..1cd665d4002 100644 --- a/services/abilitymgr/src/ability_stack_manager.cpp +++ b/services/abilitymgr/src/ability_stack_manager.cpp @@ -2850,7 +2850,7 @@ void AbilityStackManager::UninstallApp(const std::string &bundleName) CHECK_POINTER(abilityManagerService); auto handler = abilityManagerService->GetEventHandler(); CHECK_POINTER(handler); - auto task = [bundleName, this]() { AddUninstallTags(bundleName); }; + auto task = [bundleName, self = shared_from_this()]() { self->AddUninstallTags(bundleName); }; handler->PostTask(task); } diff --git a/services/abilitymgr/src/kernal_system_app_manager.cpp b/services/abilitymgr/src/kernal_system_app_manager.cpp index 0b7728f5a1a..a785eb6aca9 100644 --- a/services/abilitymgr/src/kernal_system_app_manager.cpp +++ b/services/abilitymgr/src/kernal_system_app_manager.cpp @@ -168,7 +168,14 @@ int KernalSystemAppManager::DispatchActive(const std::shared_ptr } handler->RemoveEvent(AbilityManagerService::ACTIVE_TIMEOUT_MSG, abilityRecord->GetEventId()); - auto task = [kernalManager = shared_from_this(), abilityRecord]() { kernalManager->CompleteActive(abilityRecord); }; + auto task = [weak = weak_from_this(), abilityRecord]() { + auto kernalManager = weak.lock(); + if (kernalManager == nullptr) { + HILOG_ERROR("kernalManager is null, CompleteActive failed"); + return; + } + kernalManager->CompleteActive(abilityRecord); + }; handler->PostTask(task); return ERR_OK; } @@ -182,7 +189,14 @@ void KernalSystemAppManager::CompleteActive(const std::shared_ptr auto handler = DelayedSingleton::GetInstance()->GetEventHandler(); CHECK_POINTER(handler); - auto task = [kernalManager = shared_from_this()]() { kernalManager->DequeueWaittingAbility(); }; + auto task = [weak = weak_from_this()]() { + auto kernalManager = weak.lock(); + if (kernalManager == nullptr) { + HILOG_ERROR("kernalManager is null, DequeueWaittingAbility failed"); + return; + } + kernalManager->DequeueWaittingAbility(); + }; handler->PostTask(task, "DequeueWaittingAbility"); } diff --git a/services/abilitymgr/src/lock_screen_event_subscriber.cpp b/services/abilitymgr/src/lock_screen_event_subscriber.cpp index 9ce09a1f8f5..d7744af077a 100644 --- a/services/abilitymgr/src/lock_screen_event_subscriber.cpp +++ b/services/abilitymgr/src/lock_screen_event_subscriber.cpp @@ -29,7 +29,7 @@ void LockScreenEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData & std::string eventName = data.GetWant().GetAction(); if (eventName == AbilityConfig::LOCK_SCREEN_EVENT_NAME) { bool isLockScreen = static_cast(data.GetCode()); - auto task = [this, isLockScreen]() { + auto task = [isLockScreen]() { auto service = DelayedSingleton::GetInstance(); service->UpdateLockScreenState(isLockScreen); }; diff --git a/services/abilitymgr/src/mission_list_manager.cpp b/services/abilitymgr/src/mission_list_manager.cpp index fe1fecc8649..e1980df3840 100644 --- a/services/abilitymgr/src/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission_list_manager.cpp @@ -1009,6 +1009,10 @@ int MissionListManager::ClearMission(int missionId) } std::lock_guard guard(managerLock_); auto mission = GetMissionById(missionId); + if (mission == nullptr) { + HILOG_ERROR("mission is nullptr for missionId:%{public}d", missionId); + return ERR_INVALID_VALUE; + } if (mission->GetMissionList() && mission->GetMissionList()->GetType() == MissionListType::LAUNCHER) { HILOG_ERROR("Mission id is launcher, can not clear."); return ERR_INVALID_VALUE; diff --git a/services/abilitymgr/src/mission_listener_controller.cpp b/services/abilitymgr/src/mission_listener_controller.cpp index e544c3fbbaa..ecf9de4e875 100644 --- a/services/abilitymgr/src/mission_listener_controller.cpp +++ b/services/abilitymgr/src/mission_listener_controller.cpp @@ -100,9 +100,14 @@ void MissionListenerController::NotifyMissionCreated(int32_t missionId) HILOG_ERROR("handler not init"); return; } - - auto self(shared_from_this()); - auto task = [self, missionId]() { self->NotifyListeners(missionId, Cmd::ON_MISSION_CREATED); }; + auto task = [weak = weak_from_this(), missionId]() { + auto self = weak.lock(); + if (self == nullptr) { + HILOG_ERROR("self is nullptr, NotifyMissionCreated failed."); + return; + } + self->NotifyListeners(missionId, Cmd::ON_MISSION_CREATED); + }; handler_->PostTask(task); } @@ -112,9 +117,14 @@ void MissionListenerController::NotifyMissionDestroyed(int32_t missionId) HILOG_ERROR("handler not init"); return; } - - auto self(shared_from_this()); - auto task = [self, missionId]() { self->NotifyListeners(missionId, Cmd::ON_MISSION_DESTROYED); }; + auto task = [weak = weak_from_this(), missionId]() { + auto self = weak.lock(); + if (self == nullptr) { + HILOG_ERROR("self is nullptr, NotifyMissionDestroyed failed."); + return; + } + self->NotifyListeners(missionId, Cmd::ON_MISSION_DESTROYED); + }; handler_->PostTask(task); } @@ -125,8 +135,14 @@ void MissionListenerController::NotifyMissionSnapshotChanged(int32_t missionId) return; } - auto self(shared_from_this()); - auto task = [self, missionId]() { self->NotifyListeners(missionId, Cmd::ON_MISSION_SNAPSHOT_CHANGED); }; + auto task = [weak = weak_from_this(), missionId]() { + auto self = weak.lock(); + if (self == nullptr) { + HILOG_ERROR("self is nullptr, NotifyMissionSnapshotChanged failed."); + return; + } + self->NotifyListeners(missionId, Cmd::ON_MISSION_SNAPSHOT_CHANGED); + }; handler_->PostTask(task); } @@ -137,8 +153,14 @@ void MissionListenerController::NotifyMissionMovedToFront(int32_t missionId) return; } - auto self(shared_from_this()); - auto task = [self, missionId]() { self->NotifyListeners(missionId, Cmd::ON_MISSION_MOVED_TO_FRONT); }; + auto task = [weak = weak_from_this(), missionId]() { + auto self = weak.lock(); + if (self == nullptr) { + HILOG_ERROR("self is nullptr, NotifyMissionSnapshotChanged failed."); + return; + } + self->NotifyListeners(missionId, Cmd::ON_MISSION_MOVED_TO_FRONT); + }; handler_->PostTask(task); } diff --git a/services/abilitymgr/src/pending_want_manager.cpp b/services/abilitymgr/src/pending_want_manager.cpp index 2517a73a10c..1ebe67b0e37 100644 --- a/services/abilitymgr/src/pending_want_manager.cpp +++ b/services/abilitymgr/src/pending_want_manager.cpp @@ -484,7 +484,7 @@ void PendingWantManager::ClearPendingWantRecord(const std::string &bundleName) CHECK_POINTER(abilityManagerService); auto handler = abilityManagerService->GetEventHandler(); CHECK_POINTER(handler); - auto task = [bundleName, this]() { ClearPendingWantRecordTask(bundleName); }; + auto task = [bundleName, self = shared_from_this()]() { self->ClearPendingWantRecordTask(bundleName); }; handler->PostTask(task); } -- Gitee