From 33cd7c8340a764c6209e06261192f921966c5b13 Mon Sep 17 00:00:00 2001 From: casey Date: Sun, 12 Jun 2016 17:43:13 -0700 Subject: [PATCH] Fixed a race condition in CategoryListView. --- src/musikbox/app/layout/BrowseLayout.cpp | 1 - src/musikbox/app/window/CategoryListView.cpp | 18 +++++++++++++----- src/musikbox/app/window/CategoryListView.h | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/musikbox/app/layout/BrowseLayout.cpp b/src/musikbox/app/layout/BrowseLayout.cpp index b9982be5e..1180dc037 100755 --- a/src/musikbox/app/layout/BrowseLayout.cpp +++ b/src/musikbox/app/layout/BrowseLayout.cpp @@ -61,7 +61,6 @@ BrowseLayout::BrowseLayout( } BrowseLayout::~BrowseLayout() { - } void BrowseLayout::Layout() { diff --git a/src/musikbox/app/window/CategoryListView.cpp b/src/musikbox/app/window/CategoryListView.cpp index cadb979d5..8a01fbfd5 100755 --- a/src/musikbox/app/window/CategoryListView.cpp +++ b/src/musikbox/app/window/CategoryListView.cpp @@ -73,6 +73,8 @@ void CategoryListView::RequeryWithField( const std::string& filter, const DBID selectAfterQuery) { + boost::mutex::scoped_lock lock(this->queryMutex); + if (this->activeQuery) { this->activeQuery->Cancel(); } @@ -117,10 +119,13 @@ void CategoryListView::SetFieldName(const std::string& fieldName) { } void CategoryListView::OnQueryCompleted(IQueryPtr query) { - if (query == this->activeQuery) { + boost::mutex::scoped_lock lock(this->queryMutex); + + auto active = this->activeQuery; + if (query == active) { int selectIndex = -1; if (this->selectAfterQuery != 0) { - selectIndex = this->activeQuery->GetIndexOf(this->selectAfterQuery); + selectIndex = active->GetIndexOf(this->selectAfterQuery); } this->PostMessage(WINDOW_MESSAGE_QUERY_COMPLETED, selectIndex); @@ -129,15 +134,18 @@ void CategoryListView::OnQueryCompleted(IQueryPtr query) { void CategoryListView::ProcessMessage(IMessage &message) { if (message.Type() == WINDOW_MESSAGE_QUERY_COMPLETED) { + boost::mutex::scoped_lock lock(this->queryMutex); + if (this->activeQuery && this->activeQuery->GetStatus() == IQuery::Finished) { this->metadata = activeQuery->GetResult(); activeQuery.reset(); /* UserData1 will be the index of the item we should select. if the value is -1, we won't go out of our way to select anything. */ - if (message.UserData1() > 0) { - this->SetSelectedIndex((int) message.UserData1()); - this->ScrollTo((int) message.UserData1()); + int selectedIndex = static_cast(message.UserData1()); + if (selectedIndex >= 0) { + this->SetSelectedIndex(selectedIndex); + this->ScrollTo(selectedIndex); } this->OnAdapterChanged(); diff --git a/src/musikbox/app/window/CategoryListView.h b/src/musikbox/app/window/CategoryListView.h index 0d8f5924b..784e74dc3 100755 --- a/src/musikbox/app/window/CategoryListView.h +++ b/src/musikbox/app/window/CategoryListView.h @@ -102,6 +102,7 @@ namespace musik { LibraryPtr library; Adapter *adapter; DBID selectAfterQuery; + boost::mutex queryMutex; std::string fieldName; std::shared_ptr activeQuery;