From f817e0de481f84699b5bbe2d72ab719fb795ee7d Mon Sep 17 00:00:00 2001 From: Jim Broadus Date: Sat, 11 Jan 2020 14:24:58 -0800 Subject: [PATCH] Prevent deletion of LibraryModel object while async query is running. Create a thread pool for each LibraryModel object and block destruction until all threads that are operating on this object are complete. Note that this is not a complete solution. The async query also uses the library backend which may still be deleted before the thread exits. This will be addressed in a future change. --- src/library/librarymodel.cpp | 8 ++++++-- src/library/librarymodel.h | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/library/librarymodel.cpp b/src/library/librarymodel.cpp index e164d6f4f..bd89c13d5 100644 --- a/src/library/librarymodel.cpp +++ b/src/library/librarymodel.cpp @@ -88,6 +88,7 @@ LibraryModel::LibraryModel(LibraryBackend* backend, Application* app, playlists_dir_icon_(IconLoader::Load("folder-sound", IconLoader::Base)), playlist_icon_(IconLoader::Load("x-clementine-albums", IconLoader::Base)), icon_cache_(new QNetworkDiskCache(this)), + thread_pool_(this), init_task_id_(-1), use_pretty_covers_(false), show_dividers_(true) { @@ -129,7 +130,10 @@ LibraryModel::LibraryModel(LibraryBackend* backend, Application* app, backend_->UpdateTotalSongCountAsync(); } -LibraryModel::~LibraryModel() { delete root_; } +LibraryModel::~LibraryModel() { + thread_pool_.waitForDone(); + delete root_; +} void LibraryModel::set_pretty_covers(bool use_pretty_covers) { if (use_pretty_covers != use_pretty_covers_) { @@ -769,7 +773,7 @@ void LibraryModel::LazyPopulate(LibraryItem* parent, bool signal) { void LibraryModel::ResetAsync() { QFuture future = - QtConcurrent::run(this, &LibraryModel::RunQuery, root_); + QtConcurrent::run(&thread_pool_, this, &LibraryModel::RunQuery, root_); NewClosure(future, this, SLOT(ResetAsyncQueryFinished(QFuture)), future); diff --git a/src/library/librarymodel.h b/src/library/librarymodel.h index fd8f49ea1..067dd087d 100644 --- a/src/library/librarymodel.h +++ b/src/library/librarymodel.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "libraryitem.h" #include "libraryquery.h" @@ -292,6 +293,8 @@ signals: QNetworkDiskCache* icon_cache_; + QThreadPool thread_pool_; + int init_task_id_; bool use_pretty_covers_;