Native menu crashing on 10.4


#1

Hi Jules,

When enabling the native menu in the jucedemo on macos 10.4, the menubar entries “Demo” and “Look-and-Feel” are duplicated at first, and the jucedemo app crashes very quickly. JuceDemo 1.52 works, and JuceDemo 1.53 does crash.

This change occured between juce 1.52 and 1.53:

version 1.52:

[code] void updateSubMenu (NSMenuItem* parentItem, const PopupMenu& menuToCopy,
const String& name, const int menuId, const int tag)
{
[parentItem setTag: tag];
NSMenu* menu = [parentItem submenu];

    [menu setTitle: juceStringToNS (name)];

    while ([menu numberOfItems] > 0)
        [menu removeItemAtIndex: 0];

    PopupMenu::MenuItemIterator iter (menuToCopy);

    while (iter.next())
        addMenuItem (iter, menu, menuId, tag);

    [menu setAutoenablesItems: false];
    [menu update];
}

[/code]

version 1.53

[code] void updateSubMenu (NSMenuItem* parentItem, const PopupMenu& menuToCopy,
const String& name, const int menuId, const int tag)
{
// Note: This method used to update the contents of the existing menu in-place, but that caused
// weird side-effects which messed-up keyboard focus when switching between windows. By creating
// a new menu and replacing the old one with it, that problem seems to be avoided…
NSMenu* menu = [[NSMenu alloc] initWithTitle: juceStringToNS (name)];

    PopupMenu::MenuItemIterator iter (menuToCopy);
    while (iter.next())
        addMenuItem (iter, menu, menuId, tag);

    [menu setAutoenablesItems: false];
    [menu update];
    [parentItem setTag: tag];
    [parentItem setSubmenu: menu];
    [menu release];
}

[/code]

Reverting this change does seem to fix the macos 10.4 issue, but the comment that you had put there seem to indicate that there was a reason for this change…


#2

Well, the new code is certainly necessary, but I guess something in 10.4 doesn’t like it.

Perhaps it could be enabled conditionally, e.g.

[code] void updateSubMenu (NSMenuItem* parentItem, const PopupMenu& menuToCopy,
const String& name, const int menuId, const int tag)
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
static bool is10_4 = (SystemStats::getOSXMinorVersionNumber() <= 4);

    if (is10_4)
    {
        [parentItem setTag: tag];
        NSMenu* menu = [parentItem submenu];

        [menu setTitle: juceStringToNS (name)];

        while ([menu numberOfItems] > 0)
            [menu removeItemAtIndex: 0];

        PopupMenu::MenuItemIterator iter (menuToCopy);

        while (iter.next())
            addMenuItem (iter, menu, menuId, tag);

        [menu setAutoenablesItems: false];
        [menu update];
        return;
    }
   #endif
    
    // Note: This method used to update the contents of the existing menu in-place, but that caused
    // weird side-effects which messed-up keyboard focus when switching between windows. By creating
    // a new menu and replacing the old one with it, that problem seems to be avoided..
    NSMenu* menu = [[NSMenu alloc] initWithTitle: juceStringToNS (name)];

    PopupMenu::MenuItemIterator iter (menuToCopy);
    while (iter.next())
        addMenuItem (iter, menu, menuId, tag);

    [menu setAutoenablesItems: false];
    [menu update];
    [parentItem setTag: tag];
    [parentItem setSubmenu: menu];
    [menu release];
}

[/code]


#3

Yes, thanks for the suggested patch ! It works fine, of course.