1
0
Fork 0

AIManager: Explicitly take SGSharedPtr

Otherwise a SGSharedPtr is implicitly created on ai_list.push_back(model). It's not clear to the caller that FGAIManager takes care of the passed raw pointer.

Also fixes a bug for swift using the passed dangling pointer after the created SGSharedPtr of FGAIManager got out of scope and deleted the resource.

As this commit mainly addresses the swift crash (also for backport to LTS) it doesn't fix other calls to ::attach(..) which might also use the raw pointer afterwards.
This commit is contained in:
Lars Toenning 2021-06-05 15:07:03 +02:00 committed by James Turner
parent 2895ea8b77
commit 8a8973fe15
4 changed files with 7 additions and 5 deletions

View file

@ -419,7 +419,7 @@ FGAIManager::updateLOD(SGPropertyNode* node)
}
void
FGAIManager::attach(FGAIBase *model)
FGAIManager::attach(const SGSharedPtr<FGAIBase> &model)
{
const char* typeString = model->getTypeString();
SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);

View file

@ -54,7 +54,7 @@ public:
static const char* staticSubsystemClassId() { return "ai-model"; }
void updateLOD(SGPropertyNode* node);
void attach(FGAIBase *model);
void attach(const SGSharedPtr<FGAIBase> &model);
const FGAIBase *calcCollision(double alt, double lat, double lon, double fuse_range);

View file

@ -40,12 +40,12 @@ bool FGSwiftAircraftManager::isInitialized() const
bool FGSwiftAircraftManager::addPlane(const std::string& callsign, const std::string& modelString)
{
this->removePlane(callsign); // Remove plane if already exists e.g. when rematching is done.
auto curAircraft = new FGAISwiftAircraft(callsign, modelString);
auto curAircraft = FGAISwiftAircraftPtr(new FGAISwiftAircraft(callsign, modelString));
globals->get_subsystem<FGAIManager>()->attach(curAircraft);
// Init props after prop-root is assigned
curAircraft->initProps();
aircraftByCallsign.insert(std::pair<std::string, FGAISwiftAircraft*>(callsign, curAircraft));
aircraftByCallsign.insert(std::pair<std::string, FGAISwiftAircraftPtr>(callsign, curAircraft));
return true;
}

View file

@ -29,6 +29,8 @@
class FGSwiftAircraftManager
{
using FGAISwiftAircraftPtr = SGSharedPtr<FGAISwiftAircraft>;
public:
FGSwiftAircraftManager();
~FGSwiftAircraftManager();
@ -44,7 +46,7 @@ public:
void setPlanesSurfaces(const std::vector<AircraftSurfaces>& surfaces);
private:
std::unordered_map<std::string, FGAISwiftAircraft*> aircraftByCallsign;
std::unordered_map<std::string, FGAISwiftAircraftPtr> aircraftByCallsign;
bool m_initialized = false;
};