[utilities/krusader] app/BookMan: BookMan: Fix a memory leak

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

BookMan: Fix a memory leak

About the memory leak that was fixed in the function `KrBookmarkHandler::
clearBookmarks(KrBookmark *root, bool removeBookmarks)`: When `removeBookmarks`
is `false`, a regular bookmark (neither a folder nor a separator) is not handled
in any `if/else if` branch. Therefore, the item is not deleted but `root->
children().erase(it)` is still executed (in the loop increment), so bookmarks
are removed from the children list without being deleted, causing a memory leak.

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

M  +7    -1    app/BookMan/krbookmarkhandler.cpp

https://invent.kde.org/utilities/krusader/-/commit/6fd66a4be6da2d2f546318ce35deb8bfd688e758

diff --git a/app/BookMan/krbookmarkhandler.cpp b/app/BookMan/krbookmarkhandler.cpp
index 236b99032..f98765a47 100644
--- a/app/BookMan/krbookmarkhandler.cpp
+++ b/app/BookMan/krbookmarkhandler.cpp
@@ -92,6 +92,10 @@ KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow)
 
 KrBookmarkHandler::~KrBookmarkHandler()
 {
+    // Manually delete the tree of folders and separators. Leave the
+    // regular bookmarks to be destroyed by their KActionCollection
+    clearBookmarks(_root, false);
+
     delete bookmarksMenu;
     delete manager;
     delete _privateCollection;
@@ -570,7 +574,9 @@ void KrBookmarkHandler::clearBookmarks(KrBookmark *root, bool removeBookmarks)
         } else if (removeBookmarks) {
             const auto widgets = bm->associatedObjects();
             for (QObject *w : widgets) {
-                qobject_cast<QWidget *>(w)->removeAction(bm);
+                if (QWidget *widget = qobject_cast<QWidget *>(w)) {
+                    widget->removeAction(bm);
+                }
             }
             delete bm;
         }
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.