From b8a3d6902e16b9159b17a8745172cdc8378ac466 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Tue, 27 Nov 2012 00:02:28 +0100 Subject: [PATCH] Stop AI aircraft's sound when removing Sound manager keeps a reference to _fx, so reducing its ref-count isn't enough - it must be removed explicitly. --- src/AIModel/AIBase.cxx | 27 ++++++++++++--------------- src/AIModel/AIBase.hxx | 1 + src/Model/acmodel.cxx | 2 +- src/Sound/fg_fx.cxx | 18 ++++++++++++------ src/Sound/fg_fx.hxx | 1 + 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index 79d6cb148..bbc1510bf 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -37,7 +37,6 @@ #include #include #include -#include #include #include @@ -76,7 +75,6 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) : _initialized(false), _modeldata(0), _fx(0) - { tgt_heading = hdg = tgt_altitude_ft = tgt_speed = 0.0; tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0; @@ -143,18 +141,7 @@ FGAIBase::~FGAIBase() { model_removed->setStringValue(props->getPath()); } - // refID=0 is supposedley impossible, refID=1 is the special ai_ac aircaft - // representing the current user, in the ATCManager. Maybe both these - // tests could die? - if (_fx && _refID != 0 && _refID != 1) { - SGSoundMgr *smgr = globals->get_soundmgr(); - if (smgr) { - std::stringstream name; - name << "aifx:"; - name << _refID; - smgr->remove(name.str()); - } - } + removeSoundFx(); if (fp) delete fp; @@ -477,8 +464,18 @@ void FGAIBase::unbind() { props->setBoolValue("/sim/controls/radar", true); + removeSoundFx(); +} + +void FGAIBase::removeSoundFx() { // drop reference to sound effects now - _fx = 0; + if (_fx) + { + // must remove explicitly - since the sound manager also keeps a reference + _fx->unbind(); + // now drop last reference - kill the object + _fx = 0; + } } double FGAIBase::UpdateRadar(FGAIManager* manager) { diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx index 3a9426140..98f889aa5 100644 --- a/src/AIModel/AIBase.hxx +++ b/src/AIModel/AIBase.hxx @@ -229,6 +229,7 @@ protected: double UpdateRadar(FGAIManager* manager); void removeModel(); + void removeSoundFx(); static int _newAIModelID(); diff --git a/src/Model/acmodel.cxx b/src/Model/acmodel.cxx index 41967c6da..b585df060 100644 --- a/src/Model/acmodel.cxx +++ b/src/Model/acmodel.cxx @@ -135,7 +135,7 @@ FGAircraftModel::bind () void FGAircraftModel::unbind () { - // No-op + _fx->unbind(); } void diff --git a/src/Sound/fg_fx.cxx b/src/Sound/fg_fx.cxx index 42b622108..d3a050426 100644 --- a/src/Sound/fg_fx.cxx +++ b/src/Sound/fg_fx.cxx @@ -61,14 +61,13 @@ FGFX::FGFX ( const std::string &refname, SGPropertyNode *props ) : _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true); _internal = _props->getNode("sim/current-view/internal", true); - SGSoundMgr *smgr = globals->get_soundmgr(); - if (!smgr) { + _smgr = globals->get_soundmgr(); + if (!_smgr) { return; } - SGSampleGroup::_smgr = smgr; - SGSampleGroup::_refname = refname; - SGSampleGroup::_smgr->add(this, refname); + _refname = refname; + _smgr->add(this, refname); if (!_is_aimodel) { @@ -77,6 +76,13 @@ FGFX::FGFX ( const std::string &refname, SGPropertyNode *props ) : } } +void FGFX::unbind() +{ + if (_smgr) + { + _smgr->remove(_refname); + } +} FGFX::~FGFX () { @@ -147,7 +153,7 @@ FGFX::reinit() } _sound.clear(); init(); -}; +} void diff --git a/src/Sound/fg_fx.hxx b/src/Sound/fg_fx.hxx index 7d5132dd9..1f5336e35 100644 --- a/src/Sound/fg_fx.hxx +++ b/src/Sound/fg_fx.hxx @@ -55,6 +55,7 @@ public: virtual void init (); virtual void reinit (); virtual void update (double dt); + void unbind(); private: -- 2.39.5