# BUG in ~ModalComponentManager() ! (and possible fix)

**URL:** <https://forum.juce.com/t/bug-in-modalcomponentmanager-and-possible-fix/42985>\
**Category:** Audio Plugins\
**Created:** [November 29, 2020, 12:55pm UTC](https://forum.juce.com/t/bug-in-modalcomponentmanager-and-possible-fix/42985 "2020-11-29T12:55:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [November 29, 2020, 12:55pm UTC](https://forum.juce.com/t/bug-in-modalcomponentmanager-and-possible-fix/42985/1 "2020-11-29T12:55:49Z")

</div>

When the program is closed from outside (regardless as application or plugin) the current modal stack is deleted, but not its containing components which are marked as “autoDelete”, especially problematic with currently active AltertWindow::showMessageBoxAsync. The result is, non deleted windows which are attached to the desktop before the app is finished.

Here is the fix @ed95 @reuk

```
ModalComponentManager::~ModalComponentManager()
{
   // add from here
   for (int i = stack.size(); --i >= 0;)
   {
       auto* item = stack.getUnchecked(i);
       std::unique_ptr<ModalItem> deleter(stack.removeAndReturn(i));
       Component::SafePointer<Component> compToDelete(item->autoDelete ? item->component : nullptr);
       compToDelete.deleteAndZero();
   }
   // until here

   stack.clear();
   clearSingletonInstance();
}
```

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [December 1, 2020, 7:55am UTC](https://forum.juce.com/t/bug-in-modalcomponentmanager-and-possible-fix/42985/2 "2020-12-01T07:55:54Z")

</div>

> <https://github.com/juce-framework/JUCE/pull/808>

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 1, 2020, 7:38pm UTC](https://forum.juce.com/t/bug-in-modalcomponentmanager-and-possible-fix/42985/3 "2020-12-01T19:38:53Z")

</div>

Thanks for the report, there’s a fix for this on `develop`:

> <https://github.com/juce-framework/JUCE/commit/9d0a97c33ceff97ad531bdf226e4ccbcfc316aaa>
