From b6e528c86af541f21c84df989b71744cafd9d689 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Thu, 23 Dec 2021 12:54:43 +0000 Subject: [PATCH] Check weak_ptr nullptr Signed-off-by: yaomanhai --- services/hilogd/log_buffer.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 2bbd775..9fc55d7 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -67,11 +67,15 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) } logReaderListMutex.lock_shared(); for (auto &itr :logReaderList) { - if (itr.lock()->readPos == it) { - itr.lock()->readPos = std::next(it); + auto reader = itr.lock(); + if (reader == nullptr) { + continue; } - if (itr.lock()->lastPos == it) { - itr.lock()->lastPos = std::next(it); + if (reader->readPos == it) { + reader->readPos = std::next(it); + } + if (reader->lastPos == it) { + reader->lastPos = std::next(it); } } logReaderListMutex.unlock_shared(); @@ -178,14 +182,17 @@ size_t HilogBuffer::Delete(uint16_t logType) } // Delete corresponding logs logReaderListMutex.lock_shared(); - for (auto itr = logReaderList.begin(); itr != logReaderList.end();) { - if ((*itr).lock()->readPos == it) { - (*itr).lock()->readPos = std::next(it); - } - if ((*itr).lock()->lastPos == it) { - (*itr).lock()->lastPos = std::next(it); + for (auto &itr :logReaderList) { + auto reader = itr.lock(); + if (reader == nullptr) { + continue; + } + if (reader->readPos == it) { + reader->readPos = std::next(it); + } + if (reader->lastPos == it) { + reader->lastPos = std::next(it); } - ++itr; } logReaderListMutex.unlock_shared(); -- Gitee