Skip to content

Commit

Permalink
fix: fix crash when closing folder list widget
Browse files Browse the repository at this point in the history
The crash occurred when the folder list widget was being destroyed while
CrumbBar was already deleted. This fix includes:

1. Add weak pointer to CrumbBar to avoid accessing deleted object
2. Move popup visible state update to folder list widget's hidden signal
3. Clean up folder list widget in UrlPushButton's destructor

Log: This ensures proper cleanup of resources and prevents accessing deleted objects.
  • Loading branch information
Johnson-zs authored and deepin-bot[bot] committed Jan 2, 2025
1 parent e2f7120 commit 97fc154
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/plugins/filemanager/dfmplugin-titlebar/views/urlpushbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QMouseEvent>
#include <QMenu>
#include <QTimer>
#include <QEventLoop>

DWIDGET_USE_NAMESPACE
using namespace dfmplugin_titlebar;
Expand Down Expand Up @@ -51,6 +52,10 @@ UrlPushButtonPrivate::UrlPushButtonPrivate(UrlPushButton *qq)

UrlPushButtonPrivate::~UrlPushButtonPrivate()
{
if (folderListWidget) {
folderListWidget->hide();
folderListWidget->deleteLater();
}
}

void UrlPushButtonPrivate::initConnect()
Expand Down Expand Up @@ -194,24 +199,40 @@ void UrlPushButtonPrivate::onSelectSubDirs()
fmWarning("Parent is not a CrumbBar !!!");
return;
}

// 如果已经显示则隐藏
if (folderListWidget && folderListWidget->isVisible()) {
folderListWidget->hide();
return;
}

if (crumbDatas.isEmpty())
return;

// 保存 crumbBar 的弱引用
QPointer<CrumbBar> weakCrumbBar(crumbBar);
crumbBar->setPopupVisible(true);

const bool leftToRight = (q->layoutDirection() == Qt::LeftToRight);
const int popupX = (leftToRight && !stacked) ? (q->width() - arrowWidth() - kBorderWidth) : 0;
const QPoint popupPos = q->parentWidget()->mapToGlobal(q->geometry().bottomLeft() + QPoint(popupX, 0));

if (!folderListWidget) {
folderListWidget = new FolderListWidget(q);
connect(folderListWidget, &FolderListWidget::urlButtonActivated, q, &UrlPushButton::urlButtonActivated);
connect(folderListWidget, &FolderListWidget::hidden, this, [this, weakCrumbBar]() {
if (weakCrumbBar) {
weakCrumbBar->setPopupVisible(false);
}
if (hoverFlag) {
hoverFlag = false;
q->update();
}
});
}
QList<CrumbData> childDatas;

if (!stacked) {
// 堆叠时显示堆叠目录
// 非堆叠时显示子目录
requestCompleteByUrl(crumbDatas.last().url);
} else {
folderListWidget->setFolderList(crumbDatas, stacked);
Expand All @@ -221,12 +242,6 @@ void UrlPushButtonPrivate::onSelectSubDirs()
QEventLoop eventLoop;
connect(folderListWidget, &FolderListWidget::hidden, &eventLoop, &QEventLoop::quit);
(void)eventLoop.exec(QEventLoop::DialogExec);

crumbBar->setPopupVisible(false);
if (hoverFlag) {
hoverFlag = false;
q->update();
}
}

void UrlPushButtonPrivate::onCompletionFound(const QStringList &stringList)
Expand Down

0 comments on commit 97fc154

Please sign in to comment.