Skip to content

Commit

Permalink
fix: [workspace]F5 shortcut held down, dde-file-manager crashes
Browse files Browse the repository at this point in the history
F5 forced refresh, filesortworker thread destructed filedataitem, causing main thread to crash

Log: F5 shortcut held down, dde-file-manager crashes
Bug: https://pms.uniontech.com/bug-view-217305.html
  • Loading branch information
liyigang1 committed Aug 31, 2023
1 parent 35702ab commit dc89fe6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ class FileItemData

}

typedef QSharedPointer<DPWORKSPACE_NAMESPACE::FileItemData> FileItemDataPointer;

#endif // FILEITEMDATA_H
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ QModelIndex FileViewModel::index(int row, int column, const QModelIndex &parent)
if (!filterSortWorker)
return QModelIndex();

FileItemData *itemData = nullptr;
FileItemDataPointer itemData = nullptr;
if (!isParentValid) {
itemData = filterSortWorker->rootData();
} else {
itemData = filterSortWorker->childData(row);
}

return createIndex(row, column, itemData);
return createIndex(row, column, itemData.data());
}

QUrl FileViewModel::rootUrl() const
Expand All @@ -98,7 +98,7 @@ QModelIndex FileViewModel::rootIndex() const

auto data = filterSortWorker->rootData();
if (data) {
return createIndex(0, 0, data);
return createIndex(0, 0, data.data());
} else {
return QModelIndex();
}
Expand Down Expand Up @@ -158,7 +158,7 @@ FileInfoPointer FileViewModel::fileInfo(const QModelIndex &index) const
return nullptr;

const QModelIndex &parentIndex = index.parent();
FileItemData *item = nullptr;
FileItemDataPointer item{ nullptr };
if (!parentIndex.isValid()) {
item = filterSortWorker->rootData();
} else {
Expand Down Expand Up @@ -272,7 +272,7 @@ QVariant FileViewModel::data(const QModelIndex &index, int role) const
if (filterSortWorker.isNull())
return QVariant();

FileItemData *itemData = nullptr;
FileItemDataPointer itemData = nullptr;
int columnRole = role;
if (!parentIndex.isValid()) {
itemData = filterSortWorker->rootData();
Expand Down Expand Up @@ -783,7 +783,7 @@ void FileViewModel::initFilterSortWork()

filterSortWorker.reset(new FileSortWorker(dirRootUrl, currentKey, filterCallback, nameFilters, currentFilters));
beginInsertRows(QModelIndex(), 0, 0);
filterSortWorker->setRootData(new FileItemData(dirRootUrl, InfoFactory::create<FileInfo>(dirRootUrl)));
filterSortWorker->setRootData(FileItemDataPointer(new FileItemData(dirRootUrl, InfoFactory::create<FileInfo>(dirRootUrl))));
endInsertRows();
filterSortWorker->setSortAgruments(order, role, Application::instance()->appAttribute(Application::kFileAndDirMixedSort).toBool());
filterSortWorker->moveToThread(filterSortThread.data());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "filesortworker.h"
#include "models/fileitemdata.h"
#include <dfm-base/base/application/application.h>
#include <dfm-base/base/schemefactory.h>
#include <dfm-base/utils/fileutils.h>
Expand Down Expand Up @@ -31,11 +30,6 @@ FileSortWorker::FileSortWorker(const QUrl &url, const QString &key, FileViewFilt
FileSortWorker::~FileSortWorker()
{
isCanceled = true;
if (rootdata) {
rootdata = nullptr;
delete rootdata;
}
qDeleteAll(childrenDataMap.values());
childrenDataMap.clear();
childrenUrlList.clear();
visibleChildren.clear();
Expand Down Expand Up @@ -91,23 +85,23 @@ int FileSortWorker::childrenCount()
return visibleChildren.count();
}

FileItemData *FileSortWorker::childData(const QUrl &url)
FileItemDataPointer FileSortWorker::childData(const QUrl &url)
{
QReadLocker lk(&childrenDataLocker);
return childrenDataMap.value(url);
}

void FileSortWorker::setRootData(FileItemData *data)
void FileSortWorker::setRootData(const FileItemDataPointer data)
{
rootdata = data;
}

FileItemData *FileSortWorker::rootData() const
FileItemDataPointer FileSortWorker::rootData() const
{
return rootdata;
}

FileItemData *FileSortWorker::childData(const int index)
FileItemDataPointer FileSortWorker::childData(const int index)
{
QUrl url;
{
Expand Down Expand Up @@ -162,11 +156,12 @@ void FileSortWorker::handleIteratorLocalChildren(const QString &key,
if (currentKey != key)
return;

childrenDataLastMap.clear();
this->children = children;
for (const auto &child : children) {
childrenUrlList.append(child->fileUrl());
QWriteLocker lk(&childrenDataLocker);
childrenDataMap.insert(child->fileUrl(), new FileItemData(child, rootdata));
childrenDataMap.insert(child->fileUrl(), FileItemDataPointer(new FileItemData(child, rootdata.data())));
}

if (isCanceled)
Expand Down Expand Up @@ -196,6 +191,7 @@ void FileSortWorker::handleSourceChildren(const QString &key,
if (currentKey != key)
return;

childrenDataLastMap.clear();
if (this->childrenUrlList.isEmpty()) {
handleIteratorLocalChildren(key, children, sortRole, sortOrder, isMixDirAndFile);
if (isFinished) {
Expand All @@ -215,7 +211,7 @@ void FileSortWorker::handleSourceChildren(const QString &key,
this->childrenUrlList.append(sortInfo->fileUrl());
{
QWriteLocker lk(&childrenDataLocker);
childrenDataMap.insert(sortInfo->fileUrl(), new FileItemData(sortInfo, rootdata));
childrenDataMap.insert(sortInfo->fileUrl(), FileItemDataPointer(new FileItemData(sortInfo, rootdata.data())));
}
if (checkFilters(sortInfo))
newChildren.append(sortInfo->fileUrl());
Expand Down Expand Up @@ -278,6 +274,8 @@ void FileSortWorker::handleIteratorChild(const QString &key, const SortInfoPoint
if (!child)
return;

childrenDataLastMap.clear();

addChild(child, info);
}

Expand All @@ -288,6 +286,7 @@ void FileSortWorker::handleIteratorChildren(const QString &key, QList<SortInfoPo
if (currentKey != key)
return;

childrenDataLastMap.clear();
int total = children.length();

int showIndex = visibleChildren.length();
Expand All @@ -310,7 +309,8 @@ void FileSortWorker::handleIteratorChildren(const QString &key, QList<SortInfoPo
childrenUrlList.append(sortInfo->fileUrl());
{
QWriteLocker lk(&childrenDataLocker);
childrenDataMap.insert(sortInfo->fileUrl(), new FileItemData(sortInfo->fileUrl(), infos.at(i), rootdata));
childrenDataMap.insert(sortInfo->fileUrl(),
FileItemDataPointer(new FileItemData(sortInfo->fileUrl(), infos.at(i), rootdata.data())));
}
if (!checkFilters(sortInfo))
continue;
Expand Down Expand Up @@ -344,7 +344,7 @@ void FileSortWorker::setFilters(QDir::Filters filters)
void FileSortWorker::setNameFilters(const QStringList &filters)
{
nameFilters = filters;
QMap<QUrl, FileItemData *>::iterator itr = childrenDataMap.begin();
QMap<QUrl, FileItemDataPointer>::iterator itr = childrenDataMap.begin();
for (; itr != childrenDataMap.end(); ++itr) {
checkNameFilters(itr.value());
}
Expand Down Expand Up @@ -625,8 +625,9 @@ void FileSortWorker::handleRefresh()
{
QWriteLocker lk(&childrenDataLocker);
childrenUrlList.clear();
qDeleteAll(childrenDataMap.values());
childrenDataLastMap = childrenDataMap;
childrenDataMap.clear();

}

if (!empty)
Expand Down Expand Up @@ -667,7 +668,7 @@ void FileSortWorker::handleFileInfoUpdated(const QUrl &url, const QString &infoP
handleUpdateFile(url);
}

void FileSortWorker::checkNameFilters(FileItemData *itemData)
void FileSortWorker::checkNameFilters(const FileItemDataPointer itemData)
{
if (!itemData || itemData->data(Global::ItemRoles::kItemFileIsDirRole).toBool() || nameFilters.isEmpty())
return;
Expand Down Expand Up @@ -936,7 +937,8 @@ void FileSortWorker::addChild(const SortInfoPointer &sortInfo, const FileInfoPoi
childrenUrlList.append(sortInfo->fileUrl());
{
QWriteLocker lk(&childrenDataLocker);
childrenDataMap.insert(sortInfo->fileUrl(), new FileItemData(sortInfo->fileUrl(), info, rootdata));
childrenDataMap.insert(sortInfo->fileUrl(),
FileItemDataPointer(new FileItemData(sortInfo->fileUrl(), info, rootdata.data())));
}
if (!checkFilters(sortInfo))
return;
Expand Down Expand Up @@ -972,13 +974,13 @@ void FileSortWorker::addChild(const SortInfoPointer &sortInfo,
childrenUrlList.append(sortInfo->fileUrl());
{
auto info = InfoFactory::create<FileInfo>(sortInfo->fileUrl());
FileItemData *item{nullptr};
FileItemDataPointer item{nullptr};
if (info) {
info->refresh();
item = new FileItemData(sortInfo->fileUrl(), info, rootdata);
item.reset(new FileItemData(sortInfo->fileUrl(), info, rootdata.data()));
item->setSortFileInfo(sortInfo);
} else {
item = new FileItemData(sortInfo, rootdata);
item.reset(new FileItemData(sortInfo, rootdata.data()));
}

QWriteLocker lk(&childrenDataLocker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FILESORTWORKER_H

#include "dfmplugin_workspace_global.h"
#include "models/fileitemdata.h"
#include <dfm-base/dfm_global_defines.h>
#include <dfm-base/interfaces/fileinfo.h>
#include <dfm-base/interfaces/abstractsortfilter.h>
Expand All @@ -21,7 +22,6 @@

using namespace dfmbase;
namespace dfmplugin_workspace {
class FileItemData;
class FileSortWorker : public QObject
{
Q_OBJECT
Expand All @@ -44,10 +44,10 @@ class FileSortWorker : public QObject
const bool isMixDirAndFile);
QUrl mapToIndex(int index);
int childrenCount();
FileItemData *childData(const int index);
FileItemData *childData(const QUrl &url);
void setRootData(FileItemData *data);
FileItemData *rootData() const;
FileItemDataPointer childData(const int index);
FileItemDataPointer childData(const QUrl &url);
void setRootData(const FileItemDataPointer data);
FileItemDataPointer rootData() const;
void cancel();
int getChildShowIndex(const QUrl &url);
QList<QUrl> getChildrenUrls();
Expand Down Expand Up @@ -115,7 +115,7 @@ public slots:
void handleFileInfoUpdated(const QUrl &url, const QString &infoPtr, const bool isLinkOrg);

private:
void checkNameFilters(FileItemData *itemData);
void checkNameFilters(const FileItemDataPointer itemData);
bool checkFilters(const SortInfoPointer &sortInfo, const bool byInfo = false);
void filterAllFiles(const bool byInfo = false);
void filterAllFilesOrdered();
Expand All @@ -142,13 +142,14 @@ public slots:
QList<SortInfoPointer> children {};
QList<QUrl> childrenUrlList {};
QReadWriteLock childrenDataLocker;
QMap<QUrl, FileItemData *> childrenDataMap {};
QMap<QUrl, FileItemDataPointer> childrenDataMap {};
QMap<QUrl, FileItemDataPointer> childrenDataLastMap {};
QList<QUrl> visibleChildren {};
QReadWriteLock locker;
AbstractSortFilterPointer sortAndFilter { nullptr };
FileViewFilterCallback filterCallback { nullptr };
QVariant filterData;
FileItemData *rootdata { nullptr };
FileItemDataPointer rootdata { nullptr };
QString currentKey;
Global::ItemRoles orgSortRole { Global::ItemRoles::kItemDisplayRole };
Qt::SortOrder sortOrder { Qt::AscendingOrder };
Expand Down

0 comments on commit dc89fe6

Please sign in to comment.