Fixed a memory leak in CONDITIONAL_MENU

CONDITIONAL_MENU::ENTRY now owns the wxItem object.
This commit is contained in:
Johannes Pfister 2020-06-04 19:08:59 +00:00 committed by Ian McInerney
parent e7dfa91525
commit 8e8979e317
2 changed files with 50 additions and 7 deletions

View File

@ -64,10 +64,10 @@ void CONDITIONAL_MENU::AddItem( int aId, const wxString& aText, const wxString&
BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
int aOrder )
{
wxMenuItem* item = new wxMenuItem( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_NORMAL );
if( aIcon )
AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
addEntry( ENTRY( item, aIcon, aCondition, aOrder, false ) );
}
@ -77,10 +77,10 @@ void CONDITIONAL_MENU::AddCheckItem( int aId, const wxString& aText, const wxStr
BITMAP_DEF aIcon, const SELECTION_CONDITION& aCondition,
int aOrder )
{
wxMenuItem* item = new wxMenuItem( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
wxMenuItem item( nullptr, aId, aText, aTooltip, wxITEM_CHECK );
if( aIcon )
AddBitmapToMenuItem( item, KiBitmap( aIcon ) );
AddBitmapToMenuItem( &item, KiBitmap( aIcon ) );
addEntry( ENTRY( item, aIcon, aCondition, aOrder, true ) );
}
@ -249,3 +249,39 @@ void CONDITIONAL_MENU::addEntry( ENTRY aEntry )
m_entries.insert( it, aEntry );
}
CONDITIONAL_MENU::ENTRY::ENTRY( const ENTRY& aEntry )
{
m_type = aEntry.m_type;
m_icon = aEntry.m_icon;
switch( aEntry.m_type )
{
case ACTION:
m_data.action = aEntry.m_data.action;
break;
case MENU:
m_data.menu = aEntry.m_data.menu;
break;
case WXITEM:
// We own the wxItem, so we need to make a new one for the new object
m_data.wxItem = new wxMenuItem( nullptr,
aEntry.m_data.wxItem->GetId(),
aEntry.m_data.wxItem->GetItemLabel(),
aEntry.m_data.wxItem->GetHelp(),
aEntry.m_data.wxItem->GetKind() );
break;
case SEPARATOR:
break; //No data to copy
}
m_condition = aEntry.m_condition;
m_order = aEntry.m_order;
m_isCheckmarkEntry = aEntry.m_isCheckmarkEntry;
}
CONDITIONAL_MENU::ENTRY::~ENTRY()
{
if( WXITEM == m_type )
delete m_data.wxItem;
}

View File

@ -160,14 +160,15 @@ private:
m_data.menu = aMenu;
}
ENTRY( wxMenuItem* aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
ENTRY( const wxMenuItem& aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
m_type( WXITEM ), m_icon( aWxMenuBitmap ),
m_condition( aCondition ),
m_order( aOrder ),
m_isCheckmarkEntry( aCheckmark )
{
m_data.wxItem = aItem;
m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
aItem.GetHelp(), aItem.GetKind() );
}
// Separator
@ -179,6 +180,10 @@ private:
{
}
ENTRY( const ENTRY& aEntry );
~ENTRY();
///> Possible entry types.
enum ENTRY_TYPE {
ACTION,
@ -239,6 +244,8 @@ private:
ENTRY_TYPE m_type;
const BITMAP_OPAQUE* m_icon;
// This class owns the wxItem object and needs to create, copy and delete it accordingly
// But it does not own the action nor menu item
union {
const TOOL_ACTION* action;
ACTION_MENU* menu;