SubsystemMgr: prevent double delete and use shared pointers.

This commit is contained in:
Thomas Geymayer 2015-11-19 23:10:11 +01:00
parent 21e6dd34b2
commit 10e6bbc2c5
2 changed files with 8 additions and 11 deletions

View File

@ -301,8 +301,6 @@ SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
double min_step_sec) double min_step_sec)
{ {
Member * member = get_member(name, true); Member * member = get_member(name, true);
if (member->subsystem != 0)
delete member->subsystem;
member->name = name; member->name = name;
member->subsystem = subsystem; member->subsystem = subsystem;
member->min_step_sec = min_step_sec; member->min_step_sec = min_step_sec;
@ -430,11 +428,11 @@ SGSubsystemGroup::Member::update (double delta_time_sec)
SGSubsystemMgr::SGSubsystemMgr () : SGSubsystemMgr::SGSubsystemMgr () :
_groups(MAX_GROUPS),
_initPosition(0) _initPosition(0)
{ {
for (int i = 0; i < MAX_GROUPS; i++) { for (int i = 0; i < MAX_GROUPS; i++)
_groups[i] = new SGSubsystemGroup; _groups[i].reset(new SGSubsystemGroup);
}
} }
SGSubsystemMgr::~SGSubsystemMgr () SGSubsystemMgr::~SGSubsystemMgr ()
@ -442,10 +440,7 @@ SGSubsystemMgr::~SGSubsystemMgr ()
// ensure get_subsystem returns NULL from now onwards, // ensure get_subsystem returns NULL from now onwards,
// before the SGSubsystemGroup destructors are run // before the SGSubsystemGroup destructors are run
_subsystem_map.clear(); _subsystem_map.clear();
_groups.clear();
for (int i = 0; i < MAX_GROUPS; i++) {
delete _groups[i];
}
} }
void void

View File

@ -345,6 +345,8 @@ private:
unsigned int _initPosition; unsigned int _initPosition;
}; };
typedef SGSharedPtr<SGSubsystemGroup> SGSubsystemGroupRef;
/** /**
* Manage subsystems for FlightGear. * Manage subsystems for FlightGear.
* *
@ -413,9 +415,9 @@ public:
void setReportTimingCb(void* userData,SGSubsystemTimingCb cb) {reportTimingCb = cb;reportTimingUserData = userData;} void setReportTimingCb(void* userData,SGSubsystemTimingCb cb) {reportTimingCb = cb;reportTimingUserData = userData;}
private: private:
SGSubsystemGroup* _groups[MAX_GROUPS]; std::vector<SGSubsystemGroupRef> _groups;
unsigned int _initPosition; unsigned int _initPosition;
// non-owning reference // non-owning reference
typedef std::map<std::string, SGSubsystem*> SubsystemDict; typedef std::map<std::string, SGSubsystem*> SubsystemDict;
SubsystemDict _subsystem_map; SubsystemDict _subsystem_map;