From acdeaa715e1ac080a295d453577a0934f9175073 Mon Sep 17 00:00:00 2001 From: keldorkatarn Date: Sat, 28 Apr 2018 00:42:55 +0200 Subject: [PATCH] Tracerestrict slots: Fix a crash bug when selling a train that's in a tracerestrict slot. Again a use of an invalid iterator. We need to check FIRST if range.first and reange.second are equal. After erasing the range those iterators are invalid. (cherry picked from commit 7aea2ced3ced5ec4201fbad9c206451aad057718) --- src/tracerestrict.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/tracerestrict.cpp b/src/tracerestrict.cpp index 9056939e61..572b3db966 100644 --- a/src/tracerestrict.cpp +++ b/src/tracerestrict.cpp @@ -1563,15 +1563,20 @@ void TraceRestrictSlot::PreCleanPool() } /** Remove vehicle ID from all slot occupants */ -void TraceRestrictRemoveVehicleFromAllSlots(VehicleID id) +void TraceRestrictRemoveVehicleFromAllSlots(VehicleID vehicle_id) { - auto range = slot_vehicle_index.equal_range(id); + const auto range = slot_vehicle_index.equal_range(vehicle_id); + for (auto it = range.first; it != range.second; ++it) { - TraceRestrictSlot *slot = TraceRestrictSlot::Get(it->second); - container_unordered_remove(slot->occupants, id); + auto slot = TraceRestrictSlot::Get(it->second); + container_unordered_remove(slot->occupants, vehicle_id); } + + const bool anything_to_erase = range.first != range.second; + slot_vehicle_index.erase(range.first, range.second); - if (range.first != range.second) InvalidateWindowClassesData(WC_TRACE_RESTRICT_SLOTS); + + if (anything_to_erase) InvalidateWindowClassesData(WC_TRACE_RESTRICT_SLOTS); } /** Replace all instance of a vehicle ID with another, in all slot occupants */