diff --git a/gnss/aidl/default/GnssMeasurementInterface.cpp b/gnss/aidl/default/GnssMeasurementInterface.cpp index f3242138cb..db1b8c6bc5 100644 --- a/gnss/aidl/default/GnssMeasurementInterface.cpp +++ b/gnss/aidl/default/GnssMeasurementInterface.cpp @@ -146,15 +146,18 @@ void GnssMeasurementInterface::stop() { mIsActive = false; mGnss->setGnssMeasurementEnabled(false); mThreadBlocker.notify(); - for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) { + for (auto iter = mThreads.begin(); iter != mThreads.end();) { if (iter->joinable()) { - mFutures.push_back(std::async(std::launch::async, [this, iter] { - iter->join(); - mThreads.erase(iter); - })); - } else { - mThreads.erase(iter); + // Store the thread object by value + std::thread threadToMove = std::move(*iter); + + mFutures.push_back(std::async(std::launch::async, + [threadToMove = std::move(threadToMove)]() mutable { + ALOGD("joining thread"); + threadToMove.join(); + })); } + iter = mThreads.erase(iter); } } diff --git a/gnss/aidl/default/GnssNavigationMessageInterface.cpp b/gnss/aidl/default/GnssNavigationMessageInterface.cpp index c262dc6b88..eb1d6556b6 100644 --- a/gnss/aidl/default/GnssNavigationMessageInterface.cpp +++ b/gnss/aidl/default/GnssNavigationMessageInterface.cpp @@ -90,15 +90,18 @@ void GnssNavigationMessageInterface::stop() { ALOGD("stop"); mIsActive = false; mThreadBlocker.notify(); - for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) { + for (auto iter = mThreads.begin(); iter != mThreads.end();) { if (iter->joinable()) { - mFutures.push_back(std::async(std::launch::async, [this, iter] { - iter->join(); - mThreads.erase(iter); - })); - } else { - mThreads.erase(iter); + // Store the thread object by value + std::thread threadToMove = std::move(*iter); + + mFutures.push_back(std::async(std::launch::async, + [threadToMove = std::move(threadToMove)]() mutable { + ALOGD("joining thread"); + threadToMove.join(); + })); } + iter = mThreads.erase(iter); } }