From e78f1bd4eac49af2c0a5db369666a698d4d1b92c Mon Sep 17 00:00:00 2001 From: BlueCobold Date: Sun, 22 Mar 2015 07:14:08 +0100 Subject: [PATCH] Fix for the destructor of SoundBuffer. A crash appeared when a sound still had been attached at the time of destruction. --- src/SFML/Audio/SoundBuffer.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index cdd5c537..d5873fa0 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -65,8 +65,14 @@ m_sounds () // don't copy the attached sounds //////////////////////////////////////////////////////////// SoundBuffer::~SoundBuffer() { - // First detach the buffer from the sounds that use it (to avoid OpenAL errors) - for (SoundList::const_iterator it = m_sounds.begin(); it != m_sounds.end(); ++it) + // To prevent the iterator from becoming invalid, move the entire buffer to another + // container. Otherwise calling resetBuffer would result in detachSound being + // called which removes the sound from the internal list. + SoundList sounds; + sounds.swap(m_sounds); + + // Detach the buffer from the sounds that use it (to avoid OpenAL errors) + for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) (*it)->resetBuffer(); // Destroy the buffer