From fadad5d70bb6ce00f99fdc4419725334f653f6e4 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 4 Jan 2016 12:10:04 -0600 Subject: [PATCH] Make TrafficRecord own its AIAircraft. - use the ref-counting system, hopefully avoid a dangling pointer crash I encountered. (Of course it may turn into a leak) --- src/ATC/atc_mgr.cxx | 3 +-- src/ATC/atc_mgr.hxx | 2 +- src/ATC/trafficcontrol.cxx | 18 ++++++++++++++++-- src/ATC/trafficcontrol.hxx | 14 ++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/ATC/atc_mgr.cxx b/src/ATC/atc_mgr.cxx index 1b32bde4a..355eebe26 100644 --- a/src/ATC/atc_mgr.cxx +++ b/src/ATC/atc_mgr.cxx @@ -195,8 +195,7 @@ void FGATCManager::init() { void FGATCManager::shutdown() { - delete ai_ac; - ai_ac = NULL; + ai_ac.clear(); activeStations.clear(); } diff --git a/src/ATC/atc_mgr.hxx b/src/ATC/atc_mgr.hxx index 7091ed778..b8d728acd 100644 --- a/src/ATC/atc_mgr.hxx +++ b/src/ATC/atc_mgr.hxx @@ -46,7 +46,7 @@ class FGATCManager : public SGSubsystem { private: AtcVec activeStations; - FGAIAircraft* ai_ac; + SGSharedPtr ai_ac; FGATCController *controller, *prevController; // The ATC controller that is responsible for the user's aircraft. bool networkVisible; bool initSucceeded; diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index f27ca86a1..d2fc499e9 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -180,8 +180,11 @@ FGTrafficRecord::FGTrafficRecord(): allowPushback(true), priority(0), timer(0), - latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0), - aircraft(NULL) + latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0) +{ +} + +FGTrafficRecord::~FGTrafficRecord() { } @@ -208,6 +211,17 @@ void FGTrafficRecord::setPositionAndIntentions(int pos, } } } + +void FGTrafficRecord::setAircraft(FGAIAircraft *ref) +{ + aircraft = ref; +} + +FGAIAircraft* FGTrafficRecord::getAircraft() const +{ + return aircraft.ptr(); +} + /** * Check if another aircraft is ahead of the current one, and on the same * return true / false is the is/isn't the case. diff --git a/src/ATC/trafficcontrol.hxx b/src/ATC/trafficcontrol.hxx index bdedf0e1e..bbbc78539 100644 --- a/src/ATC/trafficcontrol.hxx +++ b/src/ATC/trafficcontrol.hxx @@ -161,12 +161,12 @@ private: FGATCInstruction instruction; double latitude, longitude, heading, speed, altitude, radius; std::string runway; - //FGAISchedule *trafficRef; - FGAIAircraft *aircraft; + SGSharedPtr aircraft; public: FGTrafficRecord(); + virtual ~FGTrafficRecord(); void setId(int val) { id = val; @@ -266,17 +266,15 @@ public: return runway; }; //void setCallSign(string clsgn) { callsign = clsgn; }; - void setAircraft(FGAIAircraft *ref) { - aircraft = ref; - }; + void setAircraft(FGAIAircraft *ref); + void updateState() { state++; allowTransmission=true; }; //string getCallSign() { return callsign; }; - FGAIAircraft *getAircraft() const { - return aircraft; - }; + FGAIAircraft *getAircraft() const; + int getTime() const { return timer; }; -- 2.39.5