diff --git a/frameworks/kits/ability/native/src/ability_lifecycle.cpp b/frameworks/kits/ability/native/src/ability_lifecycle.cpp index f8d39c937543c746303c9817cfed5d2db1090fab..28c7ee112f80ae7611011219eacbb124305ea801 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 b161deeeb38bf72b38fe62f829dd326a5a9b761f..f40b501af67438411bb7af039b83649a866670b3 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 c595e1b722747ebdb523f1bc04c3970f7198bdb4..25c1427e12a727c6e7f18b19feb7e9dd9a06ce3e 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 23ad30a9702d42f5b62b337c12d2a47f1c2f2b47..4b67f313aec5aa5f22db3796370ffe960a79a174 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 4ae2b321f4fbaadbaebcdbc9dc3a73d88209bb76..1cd665d4002449edaf5668a2b34e9012575553b1 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 0b7728f5a1aaf6b0d6a9cf668a6070843f2717be..a785eb6aca9cde04e0da1b527a0f54d4fe745c54 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 9ce09a1f8f5d36a492906b5df8f18c36e32426a4..d7744af077a8e6511999057d07b1c1f9944be702 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 fe1fecc8649db34b64eadb95bb836947c29e980e..e1980df3840dcba95cf79e2b20739bce83cc6557 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 e544c3fbbaad05162e062b0724305165b9c1ce94..ecf9de4e8758e35d4de48e3e6e2d7565b08965f1 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 2517a73a10cc0288031b4ce69415773a1eefdd25..1ebe67b0e3772cb2d0fef395636c9e845f0192c1 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); }