From 691abf25c5d72c27757d1392904f3e1e3fb380a4 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 10 Jun 2021 12:48:38 +0100 Subject: [PATCH] ATC: fix crashes on shutdown --- src/ATC/GroundController.cxx | 6 +++++- src/ATC/trafficcontrol.cxx | 21 ++++++++++++++++----- src/ATC/trafficcontrol.hxx | 4 ++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/ATC/GroundController.cxx b/src/ATC/GroundController.cxx index c55303199..6b1507cb7 100644 --- a/src/ATC/GroundController.cxx +++ b/src/ATC/GroundController.cxx @@ -80,6 +80,7 @@ FGGroundController::FGGroundController() : FGGroundController::~FGGroundController() { + _isDestroying = true; } void FGGroundController::init(FGAirportDynamics* aDynamics) @@ -152,7 +153,10 @@ void FGGroundController::announcePosition(int id, Search for and erase an aircraft with a certain id from the activeTraffic vector */ void FGGroundController::signOff(int id) -{ +{ + if (_isDestroying) + return; + // Search the activeTraffic vector to find a traffic vector with our id TrafficVectorIterator i = searchActiveTraffic(id); diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index fb67404e0..c2e723320 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -916,10 +916,7 @@ FGTowerController::FGTowerController(FGAirportDynamics *par) : FGTowerController::~FGTowerController() { - // to avoid the exception described in: - // https://sourceforge.net/p/flightgear/codetickets/1864/ - // we want to ensure AI aircraft signing-off is a no-op now - + _isDestroying = true; clearTrafficControllers(activeTraffic); } @@ -1057,6 +1054,10 @@ void FGTowerController::updateAircraftInformation(int id, double lat, double lon void FGTowerController::signOff(int id) { + // ensure we don't modify activeTraffic during destruction + if (_isDestroying) + return; + // Search activeTraffic for a record matching our id TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id); if (i == activeTraffic.end() || (activeTraffic.empty())) { @@ -1147,6 +1148,7 @@ FGStartupController::FGStartupController(FGAirportDynamics *par): FGStartupController::~FGStartupController() { + _isDestroying = true; clearTrafficControllers(activeTraffic); } @@ -1219,6 +1221,10 @@ FGATCInstruction FGStartupController::getInstruction(int id) void FGStartupController::signOff(int id) { + // ensure we don't modify activeTraffic during destruction + if (_isDestroying) + return; + // Search activeTraffic for a record matching our id TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id); @@ -1574,6 +1580,7 @@ FGApproachController::FGApproachController(FGAirportDynamics *par): FGApproachController::~FGApproachController() { + _isDestroying = true; clearTrafficControllers(activeTraffic); } @@ -1653,7 +1660,11 @@ void FGApproachController::updateAircraftInformation(int id, double lat, double /* Search for and erase traffic record with a specific id */ void FGApproachController::signOff(int id) { - // Search activeTraffic for a record matching our id + // ensure we don't modify activeTraffic during destruction + if (_isDestroying) + return; + + // Search activeTraffic for a record matching our id TrafficVectorIterator i = searchActiveTraffic(activeTraffic, id); if (i == activeTraffic.end() || activeTraffic.empty()) { diff --git a/src/ATC/trafficcontrol.hxx b/src/ATC/trafficcontrol.hxx index dd79e6f04..a0ba064ce 100644 --- a/src/ATC/trafficcontrol.hxx +++ b/src/ATC/trafficcontrol.hxx @@ -448,6 +448,10 @@ public: virtual void update(double) = 0; +protected: + // guard variable to avoid modifying state during destruction + bool _isDestroying = false; + private: AtcMsgDir lastTransmissionDirection;