Not able to destroy MenuBarModel


#1

Hi everybody,

I built an application which has a menuBar. The ApplicationCommandTarget is the mainComponent. The menu is defined in another file and is built exactly the same way as in the demo. Basically in the file MainComponent.h I have:

static std::unique_ptr<ApplicationCommandManager> applicationCommandManager;

class MainContentComponent final  : public Component,
                                    public ApplicationCommandTarget
{
public:
      MainContentComponent()
      {
           applicationCommandManager->registerAllCommandsForTarget(this);
           applicationCommandManager->registerAllCommandsForTarget(JUCEApplication::getInstance());
           addAndMakeVisible(&menubar);
      }
      ...all the methods that implement commands...
private:
     MenuComponent menubar;
     ...other members....
}

and in the file menuComponent.h

class MenuComponent final : public Component,
				            public MenuBarModel
{
public:
    MenuComponent()
        {
            menuBar.reset(new MenuBarComponent(this));
            addAndMakeVisible(menuBar.get());

            setApplicationCommandManagerToWatch(&getApplicationCommandManager());
        }

      //The following methods are implemented as in the demo

     StringArray getMenuBarNames() override;
     PopupMenu getMenuForIndex(int menuIndex, const String &) override;
     void menuItemSelected(int menuItemID, int /*topLevelMenuIndex*/) override;
private:
     std::unique_ptr<MenuBarComponent> menuBar;

     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MenuComponent)
};

However when exiting the program, approximately 50% of times, I get an access violation error, and the stack calls are the followings:

> Grafia.exe!juce::Array<juce::ApplicationCommandManagerListener *,juce::DummyCriticalSection,0>::removeFirstMatchingValue(juce::ApplicationCommandManagerListener * valueToRemove) Line 928 C++
Grafia.exe!juce::MenuBarModel::~MenuBarModel() Line 37 C++
Grafia.exe!MenuComponent::~MenuComponent() Line 42 C++
Grafia.exe!MainContentComponent::~MainContentComponent() Line 759 C++

The exception in particuar is thrown here, in the file juce_Array.h:

void removeFirstMatchingValue (ParameterType valueToRemove)
{
    const ScopedLockType lock (getLock());
    auto* e = data.elements.get();

	int i;

	for (i = 0; i < numUsed; ++i)
	{
>>		if (valueToRemove == e[i]) //i has random value
		{
			removeInternal(i);
			break;
		}	
	}
}

Any help is very much appreciated.

Thanks in advance!


#2

Studying the problem more in depth it looks the application crashes only when it is closed before giving it the time to build everything. Is there any way to impede to the application to close before everything is correctly built?