From 2f3e4044b740581c54a38dc83b2b8fd44c01f823 Mon Sep 17 00:00:00 2001 From: keldorkatarn Date: Wed, 11 Apr 2018 21:25:15 +0200 Subject: [PATCH 1/2] FIX: TraceRestrictSlots: Fix crash when removing vehicle from slot. Invalid iterator got used. (it was no longer valid after calling erase with it) (cherry picked from commit 3571f1bc898b9ffd8b124040882caa5357085f42) Fixes: #37 --- src/tracerestrict.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tracerestrict.cpp b/src/tracerestrict.cpp index 9b95b8fb3d..a2960006e7 100644 --- a/src/tracerestrict.cpp +++ b/src/tracerestrict.cpp @@ -1462,8 +1462,11 @@ void TraceRestrictSlot::DeIndex(VehicleID id) auto range = slot_vehicle_index.equal_range(id); for (auto it = range.first; it != range.second; ++it) { if (it->second == this->index) { + bool is_first_in_range = (it == range.first); + auto next = slot_vehicle_index.erase(it); - if (it == range.first && next == range.second) { + + if (is_first_in_range && next == range.second) { /* Only one item, which we've just erased, clear the vehicle flag */ ClrBit(Train::Get(id)->flags, VRF_HAVE_SLOT); } From f10840438e971fccde14982dfbdb5827a94383bb Mon Sep 17 00:00:00 2001 From: keldorkatarn Date: Wed, 11 Apr 2018 21:24:16 +0200 Subject: [PATCH 2/2] TraceRestrictSlots: Fix highlighting behavior. Marking group ALL_SLOTS didn't reset the selected slot and dragging from ALL to ALL threw an unnecessary error. (cherry picked from commit c76b560134bd8d9869ead872f7a434e61a5c5a48) Fixes: #38 --- src/tracerestrict_gui.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/tracerestrict_gui.cpp b/src/tracerestrict_gui.cpp index daf25eb9d6..5dc1aecbc6 100644 --- a/src/tracerestrict_gui.cpp +++ b/src/tracerestrict_gui.cpp @@ -2915,6 +2915,7 @@ public: case WID_TRSL_ALL_VEHICLES: // All vehicles button if (this->vli.index != ALL_TRAINS_TRACE_RESTRICT_SLOT_ID) { this->vli.index = ALL_TRAINS_TRACE_RESTRICT_SLOT_ID; + this->slot_sel = INVALID_TRACE_RESTRICT_SLOT_ID; this->vehicles.ForceRebuild(); this->SetDirty(); } @@ -2973,12 +2974,14 @@ public: { switch (widget) { case WID_TRSL_ALL_VEHICLES: // All vehicles - DoCommandP(0, this->slot_sel, this->vehicle_sel, CMD_REMOVE_VEHICLE_TRACERESTRICT_SLOT | CMD_MSG(STR_TRACE_RESTRICT_ERROR_SLOT_CAN_T_REMOVE_VEHICLE)); + if (this->slot_sel != INVALID_TRACE_RESTRICT_SLOT_ID) { + DoCommandP(0, this->slot_sel, this->vehicle_sel, CMD_REMOVE_VEHICLE_TRACERESTRICT_SLOT | CMD_MSG(STR_TRACE_RESTRICT_ERROR_SLOT_CAN_T_REMOVE_VEHICLE)); - this->vehicle_sel = INVALID_VEHICLE; - this->slot_over = INVALID_GROUP; + this->vehicle_sel = INVALID_VEHICLE; + this->slot_over = INVALID_GROUP; - this->SetDirty(); + this->SetDirty(); + } break; case WID_TRSL_LIST_SLOTS: { // Matrix slot