From c00187eaeb7ae96102da5bc930caa398faa95eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83t=C4=83lin=20R=C4=83ceanu?= Date: Wed, 27 Feb 2019 02:07:06 +0200 Subject: [PATCH] Do not leak menus and menu items --- src/qt/menu.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/qt/menu.cpp b/src/qt/menu.cpp index 0619cf4c48..61d9d5935f 100644 --- a/src/qt/menu.cpp +++ b/src/qt/menu.cpp @@ -88,6 +88,12 @@ static void InsertMenuItemAction( const wxMenu *menu, const wxMenuItem *previous break; case wxITEM_NORMAL: { + // If the inserted action is a submenu, set the owner for the submenu. + if ( item->IsSubMenu() ) + { + item->GetSubMenu()->GetHandle()->setParent(qtMenu, Qt::Popup); + } + wxWindowID id = item->GetId(); if ( wxIsStockID( id ) ) { @@ -113,6 +119,9 @@ static void InsertMenuItemAction( const wxMenu *menu, const wxMenuItem *previous // Insert the action into the actual menu: QAction *successiveItemAction = ( successiveItem != NULL ) ? successiveItem->GetHandle() : NULL; qtMenu->insertAction( successiveItemAction, itemAction ); + // Menu items in Qt can be part of multiple menus, so a menu will not take ownership + // when one is added to it. Take it explicitly, otherwise it will create a memory leak. + itemAction->setParent(qtMenu); } wxMenuItem *wxMenu::DoAppend(wxMenuItem *item) @@ -211,6 +220,9 @@ bool wxMenuBar::Append( wxMenu *menu, const wxString& title ) QMenu *qtMenu = SetTitle( menu, title ); m_qtMenuBar->addMenu( qtMenu ); + // Menus in Qt can be reused as popups, so a menu bar will not take ownership when + // one is added to it. Take it explicitly, otherwise there will be a memory leak. + qtMenu->setParent(m_qtMenuBar, Qt::Popup); // must specify window type for correct display! return true; } @@ -233,6 +245,7 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) QMenu *qtMenu = SetTitle( menu, title ); QAction *qtAction = GetActionAt( m_qtMenuBar, pos ); m_qtMenuBar->insertMenu( qtAction, qtMenu ); + qtMenu->setParent(m_qtMenuBar, Qt::Popup); // must specify window type for correct display! return true; }