1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-25 16:48:44 +02:00

Don't hold a dangling pointer to a menu bar in CommandManager

This commit is contained in:
Paul Licameli 2018-10-20 12:50:22 -04:00
parent fefc3bf35d
commit 01875db4bd
3 changed files with 13 additions and 10 deletions

View File

@ -1397,6 +1397,9 @@ void MenuCreator::CreateMenusAndCommands(AudacityProject &project)
std::unique_ptr<wxMenuBar> menubar2; std::unique_ptr<wxMenuBar> menubar2;
if( !bShowExtraMenus ) if( !bShowExtraMenus )
{ {
// Make a temporary menu bar collecting items added below.
// This bar will be discarded but other side effects on the command
// manager persist.
menubar2 = c->AddMenuBar(wxT("ext-menu")); menubar2 = c->AddMenuBar(wxT("ext-menu"));
c->SetOccultCommands(true); c->SetOccultCommands(true);
} }
@ -1791,7 +1794,7 @@ void MenuCreator::CreateMenusAndCommands(AudacityProject &project)
if (!bShowExtraMenus) if (!bShowExtraMenus)
{ {
c->SwapMenuBars(); c->PopMenuBar();
c->SetOccultCommands(false); c->SetOccultCommands(false);
} }

View File

@ -597,17 +597,17 @@ wxMenuBar * CommandManager::CurrentMenuBar() const
} }
/// ///
/// Swap the last two menu bars in the list,
/// to make the previous menu bar 'current' again.
/// Typically used to switch back and forth /// Typically used to switch back and forth
/// between adding to a hidden menu bar and /// between adding to a hidden menu bar and
/// adding to one that is visible, /// adding to one that is visible
/// ///
void CommandManager::SwapMenuBars() void CommandManager::PopMenuBar()
{ {
int l = mMenuBarList.size(); auto iter = mMenuBarList.end();
wxASSERT(l >= 2); if ( iter != mMenuBarList.begin() )
std::swap(mMenuBarList[l - 2], mMenuBarList[l - 1]); mMenuBarList.erase( --iter );
else
wxASSERT( false );
} }

View File

@ -40,7 +40,7 @@ struct MenuBarListEntry
{} {}
wxString name; wxString name;
wxMenuBar *menubar; // This structure does not assume memory ownership! wxWeakRef<wxMenuBar> menubar; // This structure does not assume memory ownership!
}; };
struct SubMenuListEntry struct SubMenuListEntry
@ -211,7 +211,7 @@ class AUDACITY_DLL_API CommandManager final : public XMLTagHandler
const wxChar *accel, const wxChar *accel,
CommandFlag flags); CommandFlag flags);
void SwapMenuBars(); void PopMenuBar();
void SetOccultCommands( bool bOccult); void SetOccultCommands( bool bOccult);