# Potential crash bug in PopupMenu::HelperClasses::MenuWindow::hide()

**URL:** <https://forum.juce.com/t/potential-crash-bug-in-popupmenu-hide/51313>\
**Category:** General JUCE discussion\
**Created:** [May 5, 2022, 1:53pm UTC](https://forum.juce.com/t/potential-crash-bug-in-popupmenu-hide/51313 "2022-05-05T13:53:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benstaton](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@benstaton](https://forum.juce.com/u/benstaton)\
**Post date:** [May 5, 2022, 1:53pm UTC](https://forum.juce.com/t/potential-crash-bug-in-popupmenu-hide/51313/1 "2022-05-05T13:53:58Z")

</div>

Hi all,

We were seeing intermittent crashes on Mac when closing popup menus. Turns out the issue was a member variable being accessed after the object had been destroyed. See original JUCE code and my temporary fix below:

```auto
    void hide (const PopupMenu::Item* item, bool makeInvisible)
    {
        if (isVisible())
        {
            WeakReference<Component> deletionChecker (this);

            activeSubMenu.reset();
            currentChild = nullptr;

            if (item != nullptr
                 && item->commandManager != nullptr
                 && item->itemID != 0)
            {
                *managerOfChosenCommand = item->commandManager;
            }

            auto resultID = options.hasWatchedComponentBeenDeleted() ? 0 : getResultItemID (item);

            exitModalState (resultID);
#if 1 // ⚠ TEMP JUCE BUG FIX ⚠
			// Closing popup menus would occasionally cause an unhandled exception/crash on Mac.
			// Xcode Address Sanitizer flagged a "Use of deallocated memory" bug here.
			if (deletionChecker != nullptr)
            	exitingModalState = true;
#else
            // Original JUCE code.
            exitingModalState = true;
#endif

            if (makeInvisible && deletionChecker != nullptr)
                setVisible (false);

            if (resultID != 0
                 && item != nullptr
                 && item->action != nullptr)
                MessageManager::callAsync (item->action);
        }
    }

```

Would like to see that fixed in the next release please.

Many thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [May 6, 2022, 1:29pm UTC](https://forum.juce.com/t/potential-crash-bug-in-popupmenu-hide/51313/2 "2022-05-06T13:29:05Z")

</div>

Thanks for reporting, fixed here:

> <https://github.com/juce-framework/JUCE/commit/319da1608d5ca0f15e642c8a81ca0942355fe4c9>
