Do not leak menus and menu items

This commit is contained in:
Cătălin Răceanu
2019-02-27 02:07:06 +02:00
parent 82523b3d3b
commit c00187eaeb

View File

@@ -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;
}