From 4f747afae591f244164df25d96a83f417e6c0698 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Thu, 15 Jan 2015 17:00:13 +0100 Subject: [PATCH] rhbz#1175027: sw: fix life cycle of SwConnectionDisposedListener_Impl The SwDBManager is owned by SwDoc, so if the database thing is still alive when SwDoc dies (which may or may not indicate a leak), the listener will crash when the database stuff is eventually disposed during shutdown. Change-Id: I2bb5eb35f0a403374bc8f123b85ba2d7244016b0 --- sw/source/uibase/dbui/dbmgr.cxx | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx index ddcaf6e..fc79e80 100644 --- a/sw/source/uibase/dbui/dbmgr.cxx +++ b/sw/source/uibase/dbui/dbmgr.cxx @@ -198,26 +198,35 @@ bool lcl_getCountFromResultSet( sal_Int32& rCount, const uno::Reference { - SwDBManager& rDBManager; +private: + SwDBManager * m_pDBManager; virtual void SAL_CALL disposing( const EventObject& Source ) throw (RuntimeException, std::exception) SAL_OVERRIDE; + public: SwConnectionDisposedListener_Impl(SwDBManager& rMgr); virtual ~SwConnectionDisposedListener_Impl(); + void Dispose() { m_pDBManager = 0; } + }; struct SwDBManager_Impl { SwDSParam* pMergeData; AbstractMailMergeDlg* pMergeDialog; - uno::Reference xDisposeListener; + ::rtl::Reference m_xDisposeListener; SwDBManager_Impl(SwDBManager& rDBManager) :pMergeData(0) ,pMergeDialog(0) - ,xDisposeListener(new SwConnectionDisposedListener_Impl(rDBManager)) + , m_xDisposeListener(new SwConnectionDisposedListener_Impl(rDBManager)) {} + + ~SwDBManager_Impl() + { + m_xDisposeListener->Dispose(); + } }; static void lcl_InitNumberFormatter(SwDSParam& rParam, uno::Reference xSource) @@ -350,7 +359,7 @@ bool SwDBManager::MergeNew( const SwMergeDescriptor& rMergeDesc ) { uno::Reference xComponent(pInsert->xConnection, UNO_QUERY); if(xComponent.is()) - xComponent->addEventListener(pImpl->xDisposeListener); + xComponent->addEventListener(pImpl->m_xDisposeListener.get()); } catch(const Exception&) { @@ -2124,7 +2133,7 @@ uno::Reference< XConnection> SwDBManager::RegisterConnection(OUString& rDataSour { uno::Reference xComponent(pFound->xConnection, UNO_QUERY); if(xComponent.is()) - xComponent->addEventListener(pImpl->xDisposeListener); + xComponent->addEventListener(pImpl->m_xDisposeListener.get()); } catch(const Exception&) { @@ -2236,7 +2245,7 @@ SwDSParam* SwDBManager::FindDSData(const SwDBData& rData, bool bCreate) { uno::Reference xComponent(pFound->xConnection, UNO_QUERY); if(xComponent.is()) - xComponent->addEventListener(pImpl->xDisposeListener); + xComponent->addEventListener(pImpl->m_xDisposeListener.get()); } catch(const Exception&) { @@ -2273,7 +2282,7 @@ SwDSParam* SwDBManager::FindDSConnection(const OUString& rDataSource, bool bCre { uno::Reference xComponent(pFound->xConnection, UNO_QUERY); if(xComponent.is()) - xComponent->addEventListener(pImpl->xDisposeListener); + xComponent->addEventListener(pImpl->m_xDisposeListener.get()); } catch(const Exception&) { @@ -2822,27 +2831,31 @@ uno::Reference SwDBManager::createCursor(const OUString& _sDataSourc return xResultSet; } -SwConnectionDisposedListener_Impl::SwConnectionDisposedListener_Impl(SwDBManager& rMgr) : - rDBManager(rMgr) +SwConnectionDisposedListener_Impl::SwConnectionDisposedListener_Impl(SwDBManager& rManager) + : m_pDBManager(&rManager) { -}; +} SwConnectionDisposedListener_Impl::~SwConnectionDisposedListener_Impl() { -}; +} void SwConnectionDisposedListener_Impl::disposing( const EventObject& rSource ) throw (RuntimeException, std::exception) { ::SolarMutexGuard aGuard; + + if (!m_pDBManager) return; // we're disposed too! + uno::Reference xSource(rSource.Source, UNO_QUERY); - for(sal_uInt16 nPos = rDBManager.aDataSourceParams.size(); nPos; nPos--) + for (size_t nPos = m_pDBManager->aDataSourceParams.size(); nPos; nPos--) { - SwDSParam* pParam = &rDBManager.aDataSourceParams[nPos - 1]; + SwDSParam* pParam = &m_pDBManager->aDataSourceParams[nPos - 1]; if(pParam->xConnection.is() && (xSource == pParam->xConnection)) { - rDBManager.aDataSourceParams.erase(rDBManager.aDataSourceParams.begin() + nPos - 1); + m_pDBManager->aDataSourceParams.erase( + m_pDBManager->aDataSourceParams.begin() + nPos - 1); } } } -- 2.1.0