From 53d7183dc9159c5932a6884942084f82afac3bda Mon Sep 17 00:00:00 2001 From: liyigang Date: Wed, 26 Jun 2024 15:50:09 +0800 Subject: [PATCH] fix: Error in Destructuring and Exiting Iteration Thread Modify that only cached iteration threads bind this exit signal Log: Error in Destructuring and Exiting Iteration Thread Bug: https://pms.uniontech.com/bug-view-259807.html https://pms.uniontech.com/bug-view-259807.html --- .../dfmplugin-workspace/models/rootinfo.cpp | 21 +++++++++++++------ .../dfmplugin-workspace/models/rootinfo.h | 2 +- .../models/ut_rootinfo.cpp | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.cpp b/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.cpp index 77fc79b723..d92a160fb5 100644 --- a/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.cpp +++ b/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.cpp @@ -40,10 +40,24 @@ RootInfo::~RootInfo() thread->traversalThread->stop(); thread->traversalThread->wait(); } + // wait old dir iterator thread + for (const auto &thread : discardedThread) { + thread->disconnect(); + thread->stop(); + thread->wait(); + } } bool RootInfo::initThreadOfFileData(const QString &key, DFMGLOBAL_NAMESPACE::ItemRoles role, Qt::SortOrder order, bool isMixFileAndFolder) { + // clear old dir iterator thread + for (auto it = discardedThread.begin(); it != discardedThread.end(); ) { + if (!(*it)->isRunning()) { + it = discardedThread.erase(it); + } else { + it++; + } + } // create traversal thread QSharedPointer traversalThread = traversalThreads.value(key); bool isGetCache = canCache; @@ -144,14 +158,9 @@ int RootInfo::clearTraversalThread(const QString &key, const bool isRefresh) traversalThread->disconnect(this); if (traversalThread->isRunning()) { discardedThread.append(traversalThread); - connect(thread->traversalThread.data(), &TraversalDirThread::finished, this, [this, traversalThread] { - discardedThread.removeAll(traversalThread); - traversalThread->disconnect(); - }, - Qt::QueuedConnection); traversaling = false; } - thread->traversalThread->quit(); + thread->traversalThread->stop(); if (traversalThreads.isEmpty()) needStartWatcher = true; diff --git a/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.h b/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.h index 730a99c7b1..fe89597ad2 100644 --- a/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.h +++ b/src/plugins/filemanager/core/dfmplugin-workspace/models/rootinfo.h @@ -46,7 +46,7 @@ class RootInfo : public QObject bool initThreadOfFileData(const QString &key, DFMGLOBAL_NAMESPACE::ItemRoles role, Qt::SortOrder order, bool isMixFileAndFolder); void startWork(const QString &key, const bool getCache = false); - int clearTraversalThread(const QString &key, const bool isRefresh = false); + int clearTraversalThread(const QString &key, const bool isRefresh); void reset(); diff --git a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp index 42caec7199..afd2d031f7 100644 --- a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp +++ b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp @@ -173,12 +173,12 @@ TEST_F(UT_RootInfo, ClearTraversalThread) bool getCache = rootInfoObj->initThreadOfFileData(key, role, order, false); QString unexistKey("unexistThreadKey"); - int threadCount = rootInfoObj->clearTraversalThread(unexistKey); + int threadCount = rootInfoObj->clearTraversalThread(unexistKey, false); EXPECT_EQ(threadCount, 1); EXPECT_EQ(rootInfoObj->discardedThread.count(), 0); EXPECT_FALSE(calledThreadQuit); - threadCount = rootInfoObj->clearTraversalThread(key); + threadCount = rootInfoObj->clearTraversalThread(key, false); EXPECT_EQ(threadCount, 0); EXPECT_EQ(rootInfoObj->discardedThread.count(), 1); EXPECT_TRUE(calledThreadQuit);