I just want to invoke a context menu and call a callback whenever the user selects an option.
This is my mouseDown(…) code
void MainComponent::mouseDown(const MouseEvent & e)
{
if (e.mods.isPopupMenu())
{
PopupMenu menu;
menu.addItem (menuEntryToId(MenuEntry::EditCut), menuEntryToString(MenuEntry::EditCut));
menu.addItem (menuEntryToId(MenuEntry::EditCopy), menuEntryToString(MenuEntry::EditPaste));
menu.showMenuAsync(PopupMenu::Options(), [&, this](int result) {
if (result == menuEntryToId(MenuEntry::EditCut))
{
statusBarLabel.setText("Context Menu -> Cut invoked", dontSendNotification);
}
else if (result == menuEntryToId(MenuEntry::EditCopy))
{
statusBarLabel.setText("Context Menu -> Copy invoked", dontSendNotification);
}
});
}
else
{
mouseDown(e);
}
}
In this case, the context menu is shown properly and works also when user selects any entry in the context menu. But it causes segmentation fault, in the following case
- If popup menu is visible, user selects outside.
Thread 1 “MenuBar” received signal SIGSEGV, Segmentation fault.
0x000000000040d086 in MainComponent::mouseDown (this=<error reading variable: Cannot access memory at address 0x7fffff7fefd8>, e=<error reading variable: Cannot access memory at address 0x7fffff7fefd0>)
at …/…/Source/MainComponent.cpp:295
It causes the freeze of window, in the following case
- If the popup is visible, user tries to close the window.
Can someone tell me the right way to do this ?
Solution: Fixed by changing the way callback is called.
The code is here https://github.com/asit-dhal/Juce-Menubar-Demo