diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index 5f23f45e..86843482 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -85,11 +85,8 @@ void SGSampleGroup::start_playing_sample(SGSoundSample *sample) _smgr->sample_play( sample ); } -bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it) +void SGSampleGroup::check_playing_sample(SGSoundSample *sample) { - SGSoundSample *sample = sample_it->second; - bool rv = true; - // check if the sound has stopped by itself if (_smgr->is_sample_stopped(sample)) { // sample is stopped because it wasn't looping @@ -97,8 +94,7 @@ bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it) sample->no_valid_source(); _smgr->release_source( sample->get_source() ); _smgr->release_buffer( sample ); - sample_it = remove( sample_it ); - rv = false; + remove( sample->get_sample_name() ); } else if ( sample->has_changed() ) { if ( !sample->is_playing() ) { // a request to stop playing the sound has been filed. @@ -109,7 +105,6 @@ bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it) update_sample_config( sample ); } } - return rv; } void SGSampleGroup::update( double dt ) { @@ -126,23 +121,27 @@ void SGSampleGroup::update( double dt ) { _changed = false; } - for (auto current = _samples.begin(); current != _samples.end(); ) { - SGSoundSample *sample = current->second; - bool increment = true; + for (auto current : _samples) { + SGSoundSample *sample = current.second; if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) { start_playing_sample(sample); } else if ( sample->is_valid_source() ) { - increment = check_playing_sample(current); + check_playing_sample(sample); } - - if (increment) { - ++current; - } - testForMgrError("update"); } + + for (auto current : _removed_samples) { + for (auto sample = _samples.begin(); sample != _samples.end(); ) { + if (sample->second == current) { + sample = _samples.erase(sample); + } else { + ++sample; + } + } + } } // add a sound effect, return true if successful @@ -172,18 +171,12 @@ bool SGSampleGroup::remove( const std::string &refname ) { if ( sample_it->second->is_valid_buffer() ) _removed_samples.push_back( sample_it->second ); - _samples.erase( sample_it ); + // Do not erase within the loop +// _samples.erase( sample_it ); return true; } -sample_map::iterator SGSampleGroup::remove( sample_map::iterator sample_it ) { - if ( sample_it->second->is_valid_buffer() ) - _removed_samples.push_back( sample_it->second ); - - return _samples.erase( sample_it ); -} - // return true of the specified sound exists in the sound manager system bool SGSampleGroup::exists( const std::string &refname ) { diff --git a/simgear/sound/sample_group.hxx b/simgear/sound/sample_group.hxx index 60032051..d4cbb30d 100644 --- a/simgear/sound/sample_group.hxx +++ b/simgear/sound/sample_group.hxx @@ -223,8 +223,7 @@ protected: private: void cleanup_removed_samples(); void start_playing_sample(SGSoundSample *sample); - bool check_playing_sample(sample_map::iterator& sample_it ); - sample_map::iterator remove( sample_map::iterator sample_it ); + void check_playing_sample(SGSoundSample *sample); bool _changed = false; bool _pause = false;