@@ -586,24 +586,22 @@ void Thread::removeMessages(Object *receiver)
if (!receiver->pendingMessages_)
return;
- std::vector<std::unique_ptr<Message>> toDelete;
- for (std::unique_ptr<Message> &msg : data_->messages_.list_) {
- if (!msg)
- continue;
- if (msg->receiver_ != receiver)
- continue;
+ std::list<std::unique_ptr<Message>> toDelete;
+ for (auto it = data_->messages_.list_.begin(); it != data_->messages_.list_.end();) {
+ std::unique_ptr<Message> &msg = *it;
/*
* Move the message to the pending deletion list to delete it
- * after releasing the lock. The messages list element will
- * contain a null pointer, and will be removed when dispatching
- * messages.
+ * after releasing the lock.
*/
- toDelete.push_back(std::move(msg));
- receiver->pendingMessages_--;
+ if (msg && msg->receiver_ == receiver)
+ toDelete.splice(toDelete.end(), data_->messages_.list_, it++);
+ else
+ ++it;
}
- ASSERT(!receiver->pendingMessages_);
+ ASSERT(receiver->pendingMessages_ == toDelete.size());
+ receiver->pendingMessages_ = 0;
locker.unlock();
toDelete.clear();
Since the messages of a thread are contained in an `std::list`, use an `std::list` instead of an `std::vector` to collect the messages that need to be removed. By using `splice()`, this avoids any dynamic allocation. Note that this change does not cause issues in `dispatchMessages()` because before a message is dispatched, the pointer is moved-from the list, leaving it as nullptr. This means that the a message that is being dispatched is never erased, and thus the dispatching will continue as expected. Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> --- src/libcamera/base/thread.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)