TBTR: Avoid leaking virtual trains from template editing window

When pressing OK to convert to template
When (unsuccessfully) selling the head unit
This commit is contained in:
Jonathan G Rennison
2019-07-03 03:24:03 +01:00
parent 8fefef9d19
commit 85e9a0195d

View File

@@ -241,7 +241,6 @@ public:
case TCW_OK: { case TCW_OK: {
if (virtual_train != nullptr) { if (virtual_train != nullptr) {
DoCommandP(0, this->template_index, virtual_train->index, CMD_REPLACE_TEMPLATE_VEHICLE); DoCommandP(0, this->template_index, virtual_train->index, CMD_REPLACE_TEMPLATE_VEHICLE);
virtual_train = nullptr;
} else if (this->template_index != INVALID_VEHICLE) { } else if (this->template_index != INVALID_VEHICLE) {
DoCommandP(0, this->template_index, 0, CMD_DELETE_TEMPLATE_VEHICLE); DoCommandP(0, this->template_index, 0, CMD_DELETE_TEMPLATE_VEHICLE);
} }
@@ -554,6 +553,7 @@ public:
void RearrangeVirtualTrain() void RearrangeVirtualTrain()
{ {
if (!virtual_train) return;
virtual_train = virtual_train->First(); virtual_train = virtual_train->First();
assert(HasBit(virtual_train->subtype, GVSF_VIRTUAL)); assert(HasBit(virtual_train->subtype, GVSF_VIRTUAL));
} }
@@ -600,6 +600,7 @@ void CcDeleteVirtualTrain(const CommandCost &result, TileIndex tile, uint32 p1,
Window* window = FindWindowById(WC_CREATE_TEMPLATE, 0); Window* window = FindWindowById(WC_CREATE_TEMPLATE, 0);
if (window) { if (window) {
((TemplateCreateWindow*)window)->RearrangeVirtualTrain();
window->InvalidateData(); window->InvalidateData();
} }
} }