From 2b63e35ded6e39b7971116e6086d955916520851 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 8 Oct 2023 00:34:36 -0700 Subject: [PATCH] [ntcore] Fix moving outgoing queue to new period (#5735) std::remove_if() is destructive--it can assume the removed elements are not used, but NetworkOutgoingQueue needs them to stay intact to be moved to a different queue. Use std::stable_partition() instead. --- ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h b/ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h index 52dd1f2401..f5670ae98e 100644 --- a/ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h +++ b/ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h @@ -163,8 +163,9 @@ void NetworkOutgoingQueue::SetPeriod(NT_Handle handle, if (!created && infoIt->getSecond().queueIndex != queueIndex) { // need to move any items from old queue to new queue auto& oldMsgs = m_queues[infoIt->getSecond().queueIndex].msgs; - auto it = std::remove_if(oldMsgs.begin(), oldMsgs.end(), - [&](const auto& e) { return e.handle == handle; }); + auto it = std::stable_partition( + oldMsgs.begin(), oldMsgs.end(), + [&](const auto& e) { return e.handle != handle; }); auto& newMsgs = m_queues[queueIndex].msgs; for (auto i = it, end = oldMsgs.end(); i != end; ++i) { newMsgs.emplace_back(std::move(*i));