diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 2bbd7752006a418e904da53fe2321806e018f4d4..9fc55d748f7978a879545d29c7b99ab6f3262de2 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();