Skip to content

Commit

Permalink
TrainVehicleListItem: the missing piece
Browse files Browse the repository at this point in the history
- Now closing world works as expected.
- Launching a second instance of server exits for TCP address but
doesn't crash for Trains

And finally...
- Deleting a rail Vehicle, removes it's entries in every Train it's part
of!

Previous attempts to fix crash failed like:

commit 105c51c

TrainVehicleListItem: remove assert

It's wrong because on World close, detroying() is not called.
  • Loading branch information
gfgit committed Sep 18, 2024
1 parent 7b8cd5e commit 51847b5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions server/src/train/train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void Train::destroying()
for(const auto& item : *vehicles)
{
item->vehicle->trains.removeInternal(self);
item->destroy();
}
m_world.trains->removeObject(self);
IdObject::destroying();
Expand Down
13 changes: 7 additions & 6 deletions server/src/train/trainvehiclelistitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TrainVehicleListItem::TrainVehicleListItem(const std::shared_ptr<RailVehicle> &v
return false; //Vehicle already in train

if(vehicle)
disconnectVehicle(*vehicle);
disconnectVehicle();

if(value)
connectVehicle(*value);
Expand Down Expand Up @@ -68,9 +68,7 @@ std::string TrainVehicleListItem::getObjectId() const
void TrainVehicleListItem::destroying()
{
//NOTE: we cannot normally set vehicle to nullptr (rejected by OnSet callback)
//So we mirror cleanup operations and manually reset value at end
if(vehicle)
disconnectVehicle(*vehicle.value());
disconnectVehicle();
vehicle.setValueInternal(nullptr);

Object::destroying();
Expand Down Expand Up @@ -104,11 +102,14 @@ void TrainVehicleListItem::connectVehicle(RailVehicle &object)
[this]([[maybe_unused]] Object& obj)
{
assert(vehicle.value().get() == &obj);
vehicle = nullptr;
auto self = shared_ptr<TrainVehicleListItem>();

// Remove ourselves from Train vehicles
m_parent.removeObject(self);
});
}

void TrainVehicleListItem::disconnectVehicle(RailVehicle &/*object*/)
void TrainVehicleListItem::disconnectVehicle()
{
//Disconnect from previous vehicle
m_vehiclePropertyChanged.disconnect();
Expand Down
2 changes: 1 addition & 1 deletion server/src/train/trainvehiclelistitem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TrainVehicleListItem final : public Object
void loaded() final;

void connectVehicle(RailVehicle &object);
void disconnectVehicle(RailVehicle &);
void disconnectVehicle();

public:
ObjectProperty<RailVehicle> vehicle;
Expand Down

0 comments on commit 51847b5

Please sign in to comment.