diff --git a/src/host/history.cpp b/src/host/history.cpp index 9e875b14a36..58ae5eba21c 100644 --- a/src/host/history.cpp +++ b/src/host/history.cpp @@ -343,7 +343,7 @@ CommandHistory* CommandHistory::s_Allocate(const std::wstring_view appName, cons { if (WI_IsFlagClear(it->Flags, CLE_ALLOCATED)) { - // use LRU history buffer with same app name + // use MRU history buffer with same app name if (it->IsAppNameMatch(appName)) { BestCandidate = *it; @@ -367,18 +367,28 @@ CommandHistory* CommandHistory::s_Allocate(const std::wstring_view appName, cons History._processHandle = processHandle; return &s_historyLists.emplace_front(History); } - else if (!BestCandidate.has_value() && s_historyLists.size() > 0) + + // If we have no candidate already and we need one, + // take the LRU (which is the back/last one) which isn't allocated + // and if possible the one with empty commands list. + if (!BestCandidate.has_value()) { - // If we have no candidate already and we need one, take the LRU (which is the back/last one) which isn't allocated. - for (auto it = s_historyLists.crbegin(); it != s_historyLists.crend(); it++) + auto BestCandidateIt = s_historyLists.cend(); + for (auto it = s_historyLists.cbegin(); it != s_historyLists.cend(); it++) { if (WI_IsFlagClear(it->Flags, CLE_ALLOCATED)) { - BestCandidate = *it; - s_historyLists.erase(std::next(it).base()); // trickery to turn reverse iterator into forward iterator for erase. - break; + if (it->_commands.empty() || BestCandidateIt == s_historyLists.cend() || !BestCandidateIt->_commands.empty()) + { + BestCandidateIt = it; + } } } + if (BestCandidateIt != s_historyLists.cend()) + { + BestCandidate = *BestCandidateIt; + s_historyLists.erase(BestCandidateIt); + } } // If the app name doesn't match, copy in the new app name and free the old commands.