# showMessageBoxAsync doesn't take a callback as a parameter

**URL:** <https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058>\
**Category:** General JUCE discussion\
**Created:** [March 5, 2013, 10:04pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058 "2013-03-05T22:04:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frongo](https://avatars.discourse-cdn.com/v4/letter/f/a9a28c/32.png) [@Frongo](https://forum.juce.com/u/Frongo)\
**Post date:** [March 5, 2013, 10:04pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/1 "2013-03-05T22:04:25Z")

</div>

I’m in need of a regular “OK” message box on the mobile platforms. I want to use the NativeMessageBox::showMessageBoxAsync method but it doesn’t appear to take in a callback as a parameter like the OK/Cancel and Yes/No ones do. The documentation suggests it does (maybe it did at one point). Is it like that on purpose?

---

<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:** [March 6, 2013, 9:26am UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/2 "2013-03-06T09:26:22Z")

</div>

(Oops, looks like I copy-pasted the comments for those functions without checking them, thanks, I’ll tidy it up)

Not sure why I didn’t add a callback for that one - I should do that.

---

<div class="post-metadata">

**Author:** ![Frongo](https://avatars.discourse-cdn.com/v4/letter/f/a9a28c/32.png) [@Frongo](https://forum.juce.com/u/Frongo)\
**Post date:** [March 6, 2013, 6:45pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/3 "2013-03-06T18:45:25Z")

</div>

I see you’ve added the callback to AlertWindow::showMessageBoxAsync. I made the necessary changes to the NativeMessageBox methods:

juce\_AlertWindow.h (278) - Edited the documentation for the callback param, doesn’t talk about Ok/Cancel anymore.

`@param callback if this is non-null, the box will be launched asynchronously,
                    returning immediately, and the callback will receive a call to its
                    modalStateFinished() when the box is dismissed, with its parameter
                    being 0. The callback object will be owned and deleted by the system,
                    so make sure that it works safely and doesn't keep any references
                    to objects that might be deleted before it gets called.`

juce\_AlertWindow.cpp (642) - NativeMessageBox::showMessageBoxAsync is given the callback param

[code]void AlertWindow::showMessageBoxAsync (AlertIconType iconType,  
const String& title,  
const String& message,  
const String& buttonText,  
Component\* associatedComponent,  
ModalComponentManager::Callback\* callback)  
{  
if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())  
{  
NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent, callback);  
}  
else  
{  
AlertWindowInfo info (title, message, associatedComponent, iconType, 1, callback, false);  
info.button1 = buttonText.isEmpty() ? TRANS(“ok”) : buttonText;

```
    info.invoke();
}

```

}[/code]

juce\_NativeMessageBox.h (65) - Added callback param documentation, added function parameter

` @param callback if this is non-null, the box will be launched asynchronously,
                        returning immediately, and the callback will receive a call to its
                        modalStateFinished() when the box is dismissed, with its parameter
                        being 0. The callback object will be owned and deleted by the system,
                        so make sure that it works safely and doesn't keep any references
                        to objects that might be deleted before it gets called.
*/
static void JUCE_CALLTYPE showMessageBoxAsync (AlertWindow::AlertIconType iconType,
                                                const String& title,
                                                const String& message,
                                            #if JUCE_MODAL_LOOPS_PERMITTED
                                                Component* associatedComponent = nullptr,
                                                ModalComponentManager::Callback* callback = nullptr);
                                            #else
                                                Component* associatedComponent,
                                                ModalComponentManager::Callback* callback);
                                            #endif`

juce\_win32\_Windowing.cpp (2892) - Added parameter to showMessageBoxAsync, using same message box creation code as NativeMessageBox::showOkCancelBox

[code]void JUCE\_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,  
const String& title, const String& message,  
Component\* associatedComponent,  
ModalComponentManager::Callback\* callback)  
{  
//new WindowsMessageBox (iconType, title, message, associatedComponent, MB\_OK, 0, true);

```
ScopedPointer<WindowsMessageBox> mb (new WindowsMessageBox (iconType, title, message, associatedComponent,
                                                            MB_OK, callback, callback != nullptr));
if (callback == nullptr)
{
    mb->getResult();
    return;
}

mb.release();

```

}[/code]

juce\_mac\_Windowing.mm (116) - Added parameter to showMessageBoxAsync, using same message box creation code as NativeMessageBox::showOkCancelBox

`void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
                                                          const String& title, const String& message,
                                                          Component* /*associatedComponent*/,
                                                          ModalComponentManager::Callback* callback)
{
    //new OSXMessageBox (iconType, title, message, "OK", nullptr, nullptr, nullptr, true);
    OSXMessageBox::show (iconType, title, message, callback, "OK", nullptr, nullptr);
}`

juce\_ios\_Windowing.mm (188) - Added parameter to showMessageBoxAsync, using same message box creation code as NativeMessageBox::showOkCancelBox

[code]void JUCE\_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,  
const String& title, const String& message,  
Component\* associatedComponent,  
ModalComponentManager::Callback\* callback)  
{  
//JUCE\_AUTORELEASEPOOL  
//new iOSMessageBox (title, message, @“OK”, nil, nil, 0, true);  
ScopedPointer mb (new iOSMessageBox (title, message, @“OK”, nil, nil, callback, callback != nullptr));

```
if (callback == nullptr)
{
    mb->getResult();
    return;
}

mb.release();

```

}[/code]

juce\_linux\_Windowing.cpp (3387) - Added parameter to showMessageBoxAsync, using same message box creation code as NativeMessageBox::showOkCancelBox

`void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
                                                          const String& title, const String& message,
                                                          Component* associatedComponent,
                                                          ModalComponentManager::Callback* callback)
{
    AlertWindow::showMessageBoxAsync (iconType, title, message, associatedComponent, callback);
}`

---

<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:** [March 6, 2013, 8:56pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/4 "2013-03-06T20:56:56Z")

</div>

Thanks, though there were some problems with that code - the method must always run asynchronously, but some of your code would have blocked if the callback was null. I’ve had a quick go at implementing it now if you want to try it out…

---

<div class="post-metadata">

**Author:** ![Frongo](https://avatars.discourse-cdn.com/v4/letter/f/a9a28c/32.png) [@Frongo](https://forum.juce.com/u/Frongo)\
**Post date:** [March 6, 2013, 9:30pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/5 "2013-03-06T21:30:31Z")

</div>

> [@jules](#):
>
> Thanks, though there were some problems with that code - the method must always run asynchronously, but some of your code would have blocked if the callback was null. I’ve had a quick go at implementing it now if you want to try it out…

Oh right, I hadn’t even thought about the fact that the method’s sole purpose is to be async. I’ll just blame it on copy + paste.

I will check it out, thanks.

---

<div class="post-metadata">

**Author:** ![CPB](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@CPB](https://forum.juce.com/u/CPB)\
**Post date:** [September 9, 2013, 3:16pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/6 "2013-09-09T15:16:52Z")

</div>

Unless I've very much mistaken, the callback objects can leak when using native alert windows. With a few changes, this is (fairly) easy to reproduce in the JUCE Demo.

In **WidgetsDemo.cpp** performDemoMenuItem, change the showMessageBoxAsync call to pass in a callback:

```
AlertWindow::showMessageBoxAsync (icon,
                                  "This is an AlertWindow",
                                  "And this is the AlertWindow's message.",
                                  "ok",
                                  0,
                                  ModalCallbackFunction::forComponent (asyncMsgBoxCallback, this));
```

And add an implementation for the callback function:

```
static void asyncMsgBoxCallback (int result, WidgetsDemo*)
{
}
```

&nbsp; &nbsp;&nbsp;  
By default, JUCE will use its own alert boxes, which don't leak. You can witness this by going to the Widgets page, "click for a popup menu..", "AlertWindow demonstrations", "Show an alert-window with a warning icon...".

However, if we change the default look and feel to use the OS native Alert Windows by adding the following line to **ApplicationStartup.cpp** JUCEDemoApplication::initialise

```
LookAndFeel::getDefaultLookAndFeel().setUsingNativeAlertWindows (true);
```

then the callback object will leak.

The difference seems to be due to the fact that JUCE's async alert windows are handled by the ModalComponentManager, which cleans up nicely after itself.

I'd suggest changing the weak references in **juce\_mac\_Windowing.mm** , **juce\_win32\_Windowing.cpp** and **juce\_ios\_Windowing.mm&nbsp;** from _ModalComponentManager::Callback\* callback_ to _ScopedPointer\<ModalComponentManager::Callback\> callback_, and let these native alert window&nbsp;wrapper objects take ownership of the callbacks.  
&nbsp;

---

<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:** [September 9, 2013, 8:48pm UTC](https://forum.juce.com/t/showmessageboxasync-doesnt-take-a-callback-as-a-parameter/10058/7 "2013-09-09T20:48:03Z")

</div>

Thanks, I think you're right about that - I'd better&nbsp;take a look..
