Closing dialog windows on shutdown

Hi everybody.

Let’s say I open a dialog window like that:

        DialogWindow::LaunchOptions launchOptions;

SampleEditor* se = new SampleEditor();

launchOptions.dialogTitle = ("Edit samples");
launchOptions.escapeKeyTriggersCloseButton = true;
launchOptions.resizable = false;
launchOptions.useNativeTitleBar = false;
launchOptions.useBottomRightCornerResizer = true;
launchOptions.componentToCentreAround = getParentComponent();
launchOptions.content->setSize(600, 580);
launchOptions.dialogBackgroundColour = LookAndFeel::getDefaultLookAndFeel().findColour(ResizableWindow::backgroundColourId);


How do I properly close that window (and all other probably non modal dialogs) on application shutdown?

Any hint is appreciated.


Start by not using modal loops!

Use launchAsync(), not runModal, because if you quit in the middle of the runModal call, you can get screwed. And all you need to do is keep a pointer e.g. a std::unique_ptr to the window that is returned, and make sure it gets deleted during shutdown.

Ok, thank you.

Any suggestion on how to get the pointer to the window?

If you look at the launchAsync function, you’ll see that it returns the pointer.

Thanks. Works perfectly. I did not notice that the pointer is only being returned by launchAsync and not runModal.

Picking up this thread as I’ve had some issues implementing this correctly.

Basically, if I keep a pointer (perhaps a std::unique_ptr) to the DialogWindow when calling launchAsync() and delete it on shutdown (which happens automatically with a unique_ptr), this works great if my app is terminated with the window open.

However, what happens if I close my window normally with the mouse? The DialogWindow gets deleted, so how can my pointer (stored in a class) know not to delete the window (which has already been deleted)? I am getting an unhappy BAD_ACCESS as my unique_ptr tries to delete its contents…

Do I need some kind of application-wide WindowManager class which gets notified of windows being closed via the mouse and calls release() on my unique_ptr instances for each window? Is there something simpler than this?

Many thanks in advance for any help and sorry if I’m being stupid… :slight_smile:

I solved this using the following…

  1. Create std::unique_ptr somewhere (maybe in the class that creates the DialogWindow):

std::unique_ptr<DialogWindow> dw;

  1. Then when you create your window do…
    DialogWindow::LaunchOptions options;
    // ... (create your window here)
    dw.reset (options.launchAsync());

    // attach callback to window to release std::unique_ptr when the window is closed
    ModalComponentManager::getInstance()->attachCallback (dw.get(), ModalCallbackFunction::create ([this](int r)

Hopefully this is correct and helpful; but if it is totally wrong then please do let me know how it could be better. However, it is working well for me.