Correct way to show an AlertWindow


#1

I’d like to show an alert window to the user to tell him that there are unsaved changes. I’d need 3 buttons to achieve that. Here’s my code:

ScopedPointer<AlertWindow> alertWindow = new AlertWindow("Save changes to the current project?",
															 "The current project has unsaved changed that will be lost if you don't save them.",
															 AlertWindow::InfoIcon);
alertWindow->addButton("Save and Continue", 0);
alertWindow->addButton("Cancel", 1);
alertWindow->addButton("Discard Changes", 2);
alertWindow->setUsingNativeTitleBar(true);
	
int result = alertWindow->runModalLoop();

The program crashes at the first line after the constructor when trying to add the first button.

*** Dangling pointer deletion! Class: KeyPress
JUCE Assertion failure in juce_LeakedObjectDetector.h:64

I don’t understand why this is. It appears to be about the KeyPress the addButton function has as separate pre-defined arguments (shortCutKey1 and shortCutKey2).

Grateful for all insights!


#2

I think you don’t need to use ScopedPointer in your case.
Just AlerWIndow alertWindow(—) will work.


#3

Thanks for the hint. Tried it but it didnt’t make a difference.


#4

TBH that would have surprised me…

I don’t know why this is happening, but I didn’t have problems using the static AlertWindow::showYesNoCancelBox():

auto result = AlertWindow::showYesNoCancelBox (
    AlertWindow::InfoIcon,
    "Save changes to the current project?",
    "The current project has unsaved changed that will be lost if you don't save them.",
    "Save and Continue",
    "Cancel",
    "Discard Changes");

(Haven’t tried that specific three button version lately, but in general it worked for me)

Hope that helps…


#5

Thanks Daniel! I tried switching over and it works great.
Looks like a bug in JUCE then.

I don’t need to customize further for now, so I think I’ll just use the static method.