Popup menu causes segmentation fault or windows freeze



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);


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


I think, if you want to show your Popup menu asynchronous, you need to hold it as a member variable.
Otherwise it is destroyed when the mouseDown is finished (or the block of your if statement to be precise).
Otherwise stick to showing the menu synchronous (i.e. blocking the mouseDown until a result was selected).

Hope that helps


Hi Daniel,

Thanks for the reply.
The segmentation fault was fixed by using ModalCallbackFunction::forComponent. And I also I removed else clause. It used to make it an infinite loop.