[utilities/krusader] app/BookMan: BookMan: Fix the lifetime management of a dummy menu

Toni Asensi Esteve <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit b2ad6a741e0ff768edd46b85190fa51f5a2edd6d by Toni Asensi Esteve.
Committed on 16/08/2026 at 22:14.
Pushed by asensi into branch 'master'.

BookMan: Fix the lifetime management of a dummy menu

Fix the lifetime management of a dummy menu, replacing it with
explicit lifetime control via the destructor.

Note: `new QMenu(mainWindow->widget());` caused a Use-After-Free crash when
the widget was destroyed before the KBookmarkMenu was done with the menu.
We also cannot parent it to the KrBookmarkHandler because it is a QObject,
and QMenu requires a QWidget parent.

Revision: https://invent.kde.org/utilities/krusader/-/merge_requests/193

M  +4    -5    app/BookMan/krbookmarkhandler.cpp
M  +2    -0    app/BookMan/krbookmarkhandler.h

https://invent.kde.org/utilities/krusader/-/commit/b2ad6a741e0ff768edd46b85190fa51f5a2edd6d

diff --git a/app/BookMan/krbookmarkhandler.cpp b/app/BookMan/krbookmarkhandler.cpp
index f98765a47..e0794949b 100644
--- a/app/BookMan/krbookmarkhandler.cpp
+++ b/app/BookMan/krbookmarkhandler.cpp
@@ -83,11 +83,9 @@ KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow)
     _setQuickSearchText("");
 
     // fill a dummy menu to properly init actions (allows toolbar bookmark buttons to work properly)
-    auto menu = new QMenu(mainWindow->widget());
-    bookmarksMenu = new KBookmarkMenu(manager, nullptr, menu);
-
-    populate(menu);
-    menu->deleteLater();
+    _dummyMenu = new QMenu(); // it has no parent, we explicitly control its lifetime
+    bookmarksMenu = new KBookmarkMenu(manager, nullptr, _dummyMenu);
+    populate(_dummyMenu);
 }
 
 KrBookmarkHandler::~KrBookmarkHandler()
@@ -97,6 +95,7 @@ KrBookmarkHandler::~KrBookmarkHandler()
     clearBookmarks(_root, false);
 
     delete bookmarksMenu;
+    delete _dummyMenu; // It can be safely deleted after bookmarksMenu is deleted
     delete manager;
     delete _privateCollection;
 }
diff --git a/app/BookMan/krbookmarkhandler.h b/app/BookMan/krbookmarkhandler.h
index 4de85fd2a..30164058a 100644
--- a/app/BookMan/krbookmarkhandler.h
+++ b/app/BookMan/krbookmarkhandler.h
@@ -84,6 +84,8 @@ private:
     QString _quickSearchText() const;
     static void _highlightAction(QAction *action, bool isMatched = true);
     void _resetActionTextAndHighlighting();
+
+    QMenu *_dummyMenu; ///< A dummy menu to properly init actions
 };
 
 Q_DECLARE_METATYPE(KrBookmark *)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.