diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index f7d4348f90..012ae78f50 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -154,6 +154,16 @@ CargoList::~CargoList() } } +/** + * Empty the cargo list, but don't free the cargo packets; + * the cargo packets are cleaned by CargoPacket's CleanPool. + */ +template +void CargoList::OnCleanPool() +{ + this->packets.clear(); +} + /** * Update the cached values to reflect the removal of this packet. * Decreases count and days_in_transit. diff --git a/src/cargopacket.h b/src/cargopacket.h index 72465d90dc..063896476a 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -197,6 +197,8 @@ public: ~CargoList(); + void OnCleanPool(); + /** * Returns a pointer to the cargo packet list (so you can iterate over it etc). * @return Pointer to the packet list. diff --git a/src/station.cpp b/src/station.cpp index 9f59d72f81..8933015f89 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -70,7 +70,12 @@ Station::Station(TileIndex tile) : */ Station::~Station() { - if (CleaningPool()) return; + if (CleaningPool()) { + for (CargoID c = 0; c < NUM_CARGO; c++) { + this->goods[c].cargo.OnCleanPool(); + } + return; + } while (!this->loading_vehicles.empty()) { this->loading_vehicles.front()->LeaveStation(); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index ec78d8f428..573610c6da 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -736,7 +736,10 @@ Vehicle::~Vehicle() { free(this->name); - if (CleaningPool()) return; + if (CleaningPool()) { + this->cargo.OnCleanPool(); + return; + } /* sometimes, eg. for disaster vehicles, when company bankrupts, when removing crashed/flooded vehicles, * it may happen that vehicle chain is deleted when visible */