From 4904180247c0d5745a393e3cd57eaae29f3837e5 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Wed, 4 Feb 2015 14:02:41 +0000 Subject: fix leak from framework::AddonMenuManager::BuildMenu provide a callback when a menu item gets deleted Change-Id: I5b5f1a181fb10f53f6b1fe7b5637d385e1517530 --- framework/inc/uielement/newmenucontroller.hxx | 4 --- framework/source/fwe/classes/addonmenu.cxx | 6 ++--- framework/source/fwe/classes/bmkmenu.cxx | 4 +-- framework/source/fwe/xml/menuconfiguration.cxx | 22 +++++++++++++++++ framework/source/uielement/menubarmanager.cxx | 5 ++-- framework/source/uielement/newmenucontroller.cxx | 31 ++++++++++++------------ 6 files changed, 44 insertions(+), 28 deletions(-) (limited to 'framework') diff --git a/framework/inc/uielement/newmenucontroller.hxx b/framework/inc/uielement/newmenucontroller.hxx index ad802fa0ff2a..a86d5ad97e9f 100644 --- a/framework/inc/uielement/newmenucontroller.hxx +++ b/framework/inc/uielement/newmenucontroller.hxx @@ -81,9 +81,6 @@ namespace framework private: virtual void impl_setPopupMenu() SAL_OVERRIDE; - typedef MenuConfiguration::Attributes AddInfo; - - typedef std::unordered_map< int, std::unique_ptr > AddInfoForId; void fillPopupMenu( com::sun::star::uno::Reference< com::sun::star::awt::XPopupMenu >& rPopupMenu ); void retrieveShortcutsFromConfiguration( const ::com::sun::star::uno::Reference< ::com::sun::star::ui::XAcceleratorConfiguration >& rAccelCfg, @@ -99,7 +96,6 @@ namespace framework m_bNewMenu : 1, m_bModuleIdentified : 1, m_bAcceleratorCfg : 1; - AddInfoForId m_aAddInfoForItem; OUString m_aTargetFrame; OUString m_aModuleIdentifier; OUString m_aEmptyDocURL; diff --git a/framework/source/fwe/classes/addonmenu.cxx b/framework/source/fwe/classes/addonmenu.cxx index 460104a9d119..54c1fa9089a8 100644 --- a/framework/source/fwe/classes/addonmenu.cxx +++ b/framework/source/fwe/classes/addonmenu.cxx @@ -58,10 +58,7 @@ AddonMenu::~AddonMenu() { if ( GetItemType( i ) != MenuItemType::SEPARATOR ) { - // delete user attributes created with new! sal_uInt16 nId = GetItemId( i ); - MenuConfiguration::Attributes* pUserAttributes = reinterpret_cast(GetUserValue( nId )); - delete pUserAttributes; delete GetPopupMenu( nId ); } } @@ -333,7 +330,8 @@ void AddonMenuManager::BuildMenu( PopupMenu* pCurrent // Store values from configuration to the New and Wizard menu entries to enable // sfx2 based code to support high contrast mode correctly! - pCurrentMenu->SetUserValue( nId, sal_uIntPtr( new MenuConfiguration::Attributes( aTarget, aImageId )) ); + sal_uIntPtr nAttributePtr = MenuConfiguration::Attributes::CreateAttribute(aTarget, aImageId); + pCurrentMenu->SetUserValue(nId, nAttributePtr, MenuConfiguration::Attributes::ReleaseAttribute); pCurrentMenu->SetItemCommand( nId, aURL ); if ( pSubMenu ) diff --git a/framework/source/fwe/classes/bmkmenu.cxx b/framework/source/fwe/classes/bmkmenu.cxx index bf1396b2c5dd..a3f42905b536 100644 --- a/framework/source/fwe/classes/bmkmenu.cxx +++ b/framework/source/fwe/classes/bmkmenu.cxx @@ -164,8 +164,8 @@ void BmkMenu::Initialize() else InsertItem( nId, aTitle ); - MenuConfiguration::Attributes* pUserAttributes = new MenuConfiguration::Attributes( aTargetFrame, aImageId ); - SetUserValue( nId, reinterpret_cast(pUserAttributes) ); + sal_uIntPtr nAttributePtr = MenuConfiguration::Attributes::CreateAttribute(aTargetFrame, aImageId); + SetUserValue(nId, nAttributePtr, MenuConfiguration::Attributes::ReleaseAttribute); SetItemCommand( nId, aURL ); } diff --git a/framework/source/fwe/xml/menuconfiguration.cxx b/framework/source/fwe/xml/menuconfiguration.cxx index 8eb6a6d7731c..aabbc47b9751 100644 --- a/framework/source/fwe/xml/menuconfiguration.cxx +++ b/framework/source/fwe/xml/menuconfiguration.cxx @@ -139,6 +139,28 @@ void MenuConfiguration::StoreMenuBarConfigurationToXML( } } +sal_uIntPtr MenuConfiguration::Attributes::CreateAttribute(const OUString& rFrame, const OUString& rImageIdStr) +{ + Attributes* pAttributes = new Attributes(rFrame, rImageIdStr); + pAttributes->acquire(); + return reinterpret_cast(pAttributes); +} + +sal_uIntPtr MenuConfiguration::Attributes::CreateAttribute(const css::uno::WeakReference& rDispatchProvider) +{ + Attributes* pAttributes = new Attributes(rDispatchProvider); + pAttributes->acquire(); + return reinterpret_cast(pAttributes); +} + +void MenuConfiguration::Attributes::ReleaseAttribute(sal_uIntPtr nAttributePtr) +{ + if (!nAttributePtr) + return; + Attributes* pAttributes = reinterpret_cast(nAttributePtr); + pAttributes->release(); +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/framework/source/uielement/menubarmanager.cxx b/framework/source/uielement/menubarmanager.cxx index 4fe9415c48a1..d97c96d85aa2 100644 --- a/framework/source/uielement/menubarmanager.cxx +++ b/framework/source/uielement/menubarmanager.cxx @@ -1687,9 +1687,8 @@ void MenuBarManager::FillMenu( if ( xDispatchProvider.is() ) { // Use attributes struct to transport special dispatch provider - MenuConfiguration::Attributes* pAttributes = new MenuConfiguration::Attributes; - pAttributes->xDispatchProvider = xDispatchProvider; - pMenu->SetUserValue( nId, reinterpret_cast( pAttributes )); + sal_uIntPtr nAttributePtr = MenuConfiguration::Attributes::CreateAttribute(xDispatchProvider); + pMenu->SetUserValue(nId, nAttributePtr, MenuConfiguration::Attributes::ReleaseAttribute); } // Use help command to transport module identifier diff --git a/framework/source/uielement/newmenucontroller.cxx b/framework/source/uielement/newmenucontroller.cxx index 216c7a1c1762..12e13fa02292 100644 --- a/framework/source/uielement/newmenucontroller.cxx +++ b/framework/source/uielement/newmenucontroller.cxx @@ -83,9 +83,10 @@ void NewMenuController::setMenuImages( PopupMenu* pPopupMenu, bool bSetImages ) bool bImageSet( false ); OUString aImageId; - AddInfoForId::const_iterator pInfo = m_aAddInfoForItem.find( nItemId ); - if ( pInfo != m_aAddInfoForItem.end() ) - aImageId = pInfo->second->aImageId; // Retrieve image id for menu item + sal_uIntPtr nAttributePtr = pPopupMenu->GetUserValue(sal::static_int_cast(i)); + MenuConfiguration::Attributes* pAttributes = reinterpret_cast(nAttributePtr); + if (pAttributes) + aImageId = pAttributes->aImageId; if ( !aImageId.isEmpty() ) { @@ -345,13 +346,12 @@ void NewMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopup if (( nItemId != 0 ) && ( pSubMenu->GetItemType( nItemId ) != MenuItemType::SEPARATOR )) { - MenuConfiguration::Attributes* pBmkAttributes = reinterpret_cast(pSubMenu->GetUserValue( nItemId )); - if ( pBmkAttributes != 0 ) + sal_uIntPtr nAttributePtr = pSubMenu->GetUserValue(nItemId); + if (nAttributePtr) { - auto result = m_aAddInfoForItem.insert( - std::make_pair(nItemId, std::unique_ptr(new AddInfo(*pBmkAttributes)))); - MenuConfiguration::Attributes *pNewInfo = result.first->second.get(); - pVCLPopupMenu->SetUserValue(nItemId, reinterpret_cast(pNewInfo)); + MenuConfiguration::Attributes* pAttributes = reinterpret_cast(nAttributePtr); + pAttributes->acquire(); + pVCLPopupMenu->SetUserValue(nItemId, nAttributePtr, MenuConfiguration::Attributes::ReleaseAttribute); } } } @@ -405,10 +405,16 @@ void SAL_CALL NewMenuController::itemSelected( const css::awt::MenuEvent& rEvent VCLXPopupMenu* pPopupMenu = static_cast(VCLXPopupMenu::GetImplementation( xPopupMenu )); if ( pPopupMenu ) { + OUString aTargetFrame( m_aTargetFrame ); + { SolarMutexGuard aSolarMutexGuard; PopupMenu* pVCLPopupMenu = static_cast(pPopupMenu->GetMenu()); - aTargetURL.Complete = pVCLPopupMenu->GetItemCommand( rEvent.MenuId ); + aTargetURL.Complete = pVCLPopupMenu->GetItemCommand(rEvent.MenuId); + sal_uIntPtr nAttributePtr = pVCLPopupMenu->GetUserValue(rEvent.MenuId); + MenuConfiguration::Attributes* pAttributes = reinterpret_cast(nAttributePtr); + if (pAttributes) + aTargetFrame = pAttributes->aTargetFrame; } xURLTransformer->parseStrict( aTargetURL ); @@ -416,11 +422,6 @@ void SAL_CALL NewMenuController::itemSelected( const css::awt::MenuEvent& rEvent aArgsList[0].Name = "Referer"; aArgsList[0].Value = makeAny( OUString( "private:user" )); - OUString aTargetFrame( m_aTargetFrame ); - AddInfoForId::const_iterator pItem = m_aAddInfoForItem.find( rEvent.MenuId ); - if ( pItem != m_aAddInfoForItem.end() ) - aTargetFrame = pItem->second->aTargetFrame; - xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTargetFrame, 0 ); } } -- cgit