From 314623c157d7f10ee4bd0595550d4a385711b7db Mon Sep 17 00:00:00 2001 From: Colin Wallace Date: Fri, 3 Jul 2015 03:44:29 +0000 Subject: [PATCH] fix race condition in TrackContainer::removeTrack --- src/core/TrackContainer.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/TrackContainer.cpp b/src/core/TrackContainer.cpp index 5ee6c3fd0..f209fae7d 100644 --- a/src/core/TrackContainer.cpp +++ b/src/core/TrackContainer.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "TrackContainer.h" #include "InstrumentTrack.h" @@ -174,6 +175,10 @@ void TrackContainer::addTrack( Track * _track ) void TrackContainer::removeTrack( Track * _track ) { + // need a read locker to ensure that m_tracks doesn't change after reading index. + // After checking that index != -1, we need to upgrade the lock to a write locker before changing m_tracks. + // But since Qt offers no function to promote a read lock to a write lock, we must start with the write locker. + QWriteLocker lockTracksAccess(&m_tracksMutex); int index = m_tracks.indexOf( _track ); if( index != -1 ) { @@ -181,9 +186,8 @@ void TrackContainer::removeTrack( Track * _track ) if (_track->isSolo()) { _track->setSolo(false); } - m_tracksMutex.lockForWrite(); m_tracks.remove( index ); - m_tracksMutex.unlock(); + lockTracksAccess.unlock(); if( Engine::getSong() ) {