MenuBarModel question


#1

I’m creating a menu bar for my app in my main window class, using the same calls as are found in the juce demo:

myCommandManager = new ApplicationCommandManager();
myCommandManager->registerAllCommandsForTarget(this);
myCommandManager->registerAllCommandsForTarget(JUCEApplication::getInstance());
addKeyListener(myCommandManager->getKeyMappings);
setMenuBar(this, 20);
setApplicationCommandManagerToWatch(myCommandManager);

All this works without a problem, but on quit, I throw an exception in MenuBarModel::removeListener(), because my MenuBarComponent is not in the list of listeners for my MenuBarModel.

The call to setMenuBar() creates the MenuBarComponent, and is passed my window as it’s MenuBarModel, so I would think that the DocumentWindow class would be responsible for deleting things in the right order. What has gone astray here?

In the juce example, the only difference is that the content component is the MenuBarModel and ApplicationCommandTarget. Is it wrong to add that functionality to my main window instead?


#2

I’d always recommend deregistering your objects manually at the correct time rather than leaving it up to the destructors.

You can get into nasty situations if you let an object be deregistered by its superclass’s destructor, because at the time that the deregistration happens, the object will be half-deconstructed and all hell can break loose if a method gets called.


#3

Ah, I missed the calls to setMenu(0) and setContentComponent(0) in the destructor. That fixed things.

Thanks for pointing me in the right direction.