Resolves: tdf#157258 "Always autocorrect to" deletes the word

instead of replacing it, affecting extensions like Grammalecte,
LanguageTool, Antidote

this began in:

commit afa35742a4633db31b6d6c72cf45741506e9edfb
Date:   Sat Dec 11 21:09:39 2021 +0000

    prefer more css::awt::XPopupMenu api

but the underlying trap was introduced in:

commit 6c84dc18062ec6aad71fd65a409373c274402991
Date:   Wed Oct 6 10:16:39 2010 +0100

    initial commit for vba blob ( not including container_control stuff )

which added creating a VCLXPopupMenu in VCLXMenu::getPopupMenu if there
was a vcl PopupMenu in the vcl Menu which hadn't been created by calling
VCLXMenu::setPopupMenu. That didn't take into account that VCLXPopupMenu
(like VCLXMenu) takes ownership of the PopupMenu and will destroy it in
its own dtor, so the sub menu Popup get destroyed if the VCLXPopupMenu
is shorter lived than the VCLXMenu wrapping the parent Menu.

Change-Id: Ic28c27670d846ee9d2ff77d834e43fc157924eb6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165582
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
diff --git a/toolkit/source/awt/vclxmenu.cxx b/toolkit/source/awt/vclxmenu.cxx
index 20d3d5d..65152588 100644
--- a/toolkit/source/awt/vclxmenu.cxx
+++ b/toolkit/source/awt/vclxmenu.cxx
@@ -413,11 +413,30 @@ css::uno::Reference< css::awt::XPopupMenu > VCLXMenu::getPopupMenu(
                break;
            }
        }
        // it seems the popup menu is not insert into maPopupMenuRefs
        // if the popup men is not created by stardiv.Toolkit.VCLXPopupMenu
        /*
           If the popup menu is not inserted via setPopupMenu then
           maPopupMenuRefs won't have an entry for it, so create an XPopupMenu
           for it now.

           This means that this vcl PopupMenu "pMenu" either existed as a child
           of the vcl Menu "mpMenu" before the VCLXMenu was created for that or
           it was added directly via vcl.
        */
        if( !aRef.is() )
        {
            aRef = new VCLXPopupMenu( static_cast<PopupMenu*>(pMenu) );
            /*
               In any case, the VCLXMenu has ownership of "mpMenu" and will
               destroy it in the VCLXMenu dtor.

               Similarly because VCLXPopupMenu takes ownership of the vcl
               PopupMenu "pMenu", the underlying vcl popup will be destroyed
               when VCLXPopupMenu is, so we should add it now to
               maPopupMenuRefs to ensure its lifecycle is at least bound to
               the VCLXMenu that owns the parent "mpMenu" similarly to
               PopupMenus added via the more conventional setPopupMenu.
            */
            maPopupMenuRefs.push_back( aRef );
        }
    }
    return aRef;