From 6fed76695cbab39ffbaca2cd5e656e4c5a5badca Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sun, 30 Oct 2022 23:53:02 +0800 Subject: [PATCH] Cabana: fix possible crash when removing tabs (#26283) fix dead loop --- tools/cabana/detailwidget.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/cabana/detailwidget.cc b/tools/cabana/detailwidget.cc index 59e5b5f296..57a3910303 100644 --- a/tools/cabana/detailwidget.cc +++ b/tools/cabana/detailwidget.cc @@ -104,12 +104,7 @@ DetailWidget::DetailWidget(ChartsWidget *charts, QWidget *parent) : charts(chart setMessage(tabbar->tabText(index)); } }); - QObject::connect(tabbar, &QTabBar::tabCloseRequested, [=](int index) { - if (tabbar->currentIndex() == index) { - tabbar->setCurrentIndex(index == tabbar->count() - 1 ? index - 1 : index + 1); - } - tabbar->removeTab(index); - }); + QObject::connect(tabbar, &QTabBar::tabCloseRequested, tabbar, &QTabBar::removeTab); QObject::connect(charts, &ChartsWidget::chartOpened, [this](const QString &id, const Signal *sig) { updateChartState(id, sig, true); }); QObject::connect(charts, &ChartsWidget::chartClosed, [this](const QString &id, const Signal *sig) { updateChartState(id, sig, false); }); } @@ -120,9 +115,14 @@ void DetailWidget::showTabBarContextMenu(const QPoint &pt) { QMenu menu(this); menu.addAction(tr("Close Other Tabs")); if (menu.exec(tabbar->mapToGlobal(pt))) { - for (int i = tabbar->count() - 1; i >= 0; --i) { - if (i != index) - tabbar->removeTab(i); + tabbar->setCurrentIndex(index); + // remove all tabs before the one to keep + for (int i = 0; i < index; ++i) { + tabbar->removeTab(0); + } + // remove all tabs after the one to keep + while (tabbar->count() > 1) { + tabbar->removeTab(1); } } }