From c17cd5c5d8862b0bb4086ea4bb7c26e150adea5a Mon Sep 17 00:00:00 2001 From: Zhang Sheng Date: Thu, 2 Jan 2025 18:51:31 +0800 Subject: [PATCH] fix: fix crash when closing folder list widget 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. --- .../views/urlpushbutton.cpp | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/plugins/filemanager/dfmplugin-titlebar/views/urlpushbutton.cpp b/src/plugins/filemanager/dfmplugin-titlebar/views/urlpushbutton.cpp index 053edb3681..559842045c 100644 --- a/src/plugins/filemanager/dfmplugin-titlebar/views/urlpushbutton.cpp +++ b/src/plugins/filemanager/dfmplugin-titlebar/views/urlpushbutton.cpp @@ -21,6 +21,7 @@ #include #include #include +#include DWIDGET_USE_NAMESPACE using namespace dfmplugin_titlebar; @@ -51,6 +52,10 @@ UrlPushButtonPrivate::UrlPushButtonPrivate(UrlPushButton *qq) UrlPushButtonPrivate::~UrlPushButtonPrivate() { + if (folderListWidget) { + folderListWidget->hide(); + folderListWidget->deleteLater(); + } } void UrlPushButtonPrivate::initConnect() @@ -194,13 +199,20 @@ void UrlPushButtonPrivate::onSelectSubDirs() fmWarning("Parent is not a CrumbBar !!!"); return; } + + // 如果已经显示则隐藏 if (folderListWidget && folderListWidget->isVisible()) { folderListWidget->hide(); return; } + if (crumbDatas.isEmpty()) return; + + // 保存 crumbBar 的弱引用 + QPointer 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)); @@ -208,10 +220,19 @@ void UrlPushButtonPrivate::onSelectSubDirs() 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 childDatas; + if (!stacked) { - // 堆叠时显示堆叠目录 + // 非堆叠时显示子目录 requestCompleteByUrl(crumbDatas.last().url); } else { folderListWidget->setFolderList(crumbDatas, stacked); @@ -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)