# Removing Modal Loops

**URL:** <https://forum.juce.com/t/removing-modal-loops/15712>\
**Category:** General JUCE discussion\
**Created:** [October 15, 2015, 10:54am UTC](https://forum.juce.com/t/removing-modal-loops/15712 "2015-10-15T10:54:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [October 15, 2015, 10:54am UTC](https://forum.juce.com/t/removing-modal-loops/15712/1 "2015-10-15T10:54:08Z")

</div>

Having throughly blasted my foot with bullets I'm removing all the modal loops from an application.&nbsp;

There's&nbsp;a scenario I'm still scratching my head about though. &nbsp;In the code below if the object I'm calling the AlertWindow from is deleted before the user pushes a button on the dialog, what's the tidiest way of ensuring the callback doesn't crash (as _devices_ will have been deleted). &nbsp;Do I need to use WeakReference here, or is there a less verbose solution I've missed?

I guess I could extend my ModalCallbackAction class to take a WeakReference to the originating object&nbsp;and then not call the lambda at all - which is a relatively nice solution.

```
    auto callback = [this, deviceId](int r)
    {
        if (r == 1)
        {
            auto newDevice = devices.createNewObject();
            newDevice->setUniqueDeviceId(deviceId);
            newDevice->setConnected(true);
            listeners.call(&Listener::deviceListChanged);
        }
    };

    AlertWindow::showOkCancelBox(AlertWindow::QuestionIcon, "Attach Device", "Attach?", "Yes", "No", nullptr, new ModalCallbackAction(callback)); 
```

Where:&nbsp;

```
class ModalCallbackAction
    :
    public ModalComponentManager::Callback
{
public:
    ModalCallbackAction(std::function<void(int)> actionToRun)
        :
        action(actionToRun)
    {}
    void modalStateFinished(int resultCode) override
    {
        action(resultCode);
    }
private:
    std::function<void(int)> action;
};
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 15, 2015, 10:56am UTC](https://forum.juce.com/t/removing-modal-loops/15712/2 "2015-10-15T10:56:34Z")

</div>

Try&nbsp;ModalCallbackFunction::forComponent - it'll manage the WeakReference for you so that it handles the deletion of the Component safely.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [October 15, 2015, 1:15pm UTC](https://forum.juce.com/t/removing-modal-loops/15712/3 "2015-10-15T13:15:11Z")

</div>

Ta! &nbsp;WeakReference it is then ... :) &nbsp;Unfortunately not always using&nbsp;this from a Component object so I've had to roll my own. &nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![alatar](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@alatar](https://forum.juce.com/u/alatar)\
**Post date:** [October 18, 2015, 3:45pm UTC](https://forum.juce.com/t/removing-modal-loops/15712/4 "2015-10-18T15:45:23Z")

</div>

Interesting example. I am also just looking into modal dialogs.
