make memory management more explicit in BaseContext

Change-Id: I3afdfd7b461c6eccc57c23314f992900d14bd7d8
Reviewed-on: https://gerrit.libreoffice.org/48708
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2018-01-23 15:55:36 +02:00
parent 1168956b91
commit 03f427793b
6 changed files with 93 additions and 138 deletions

View File

@@ -1174,29 +1174,27 @@ void BaseContent::endTask( sal_Int32 CommandId )
} }
ContentEventNotifier* std::unique_ptr<ContentEventNotifier>
BaseContent::cDEL() BaseContent::cDEL()
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
m_nState |= Deleted; m_nState |= Deleted;
ContentEventNotifier* p; std::unique_ptr<ContentEventNotifier> p;
if( m_pContentEventListeners ) if( m_pContentEventListeners )
{ {
p = new ContentEventNotifier( m_pMyShell, p.reset( new ContentEventNotifier( m_pMyShell,
this, this,
m_xContentIdentifier, m_xContentIdentifier,
m_pContentEventListeners->getElements() ); m_pContentEventListeners->getElements() ) );
} }
else
p = nullptr;
return p; return p;
} }
ContentEventNotifier* std::unique_ptr<ContentEventNotifier>
BaseContent::cEXC( const OUString& aNewName ) BaseContent::cEXC( const OUString& aNewName )
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
@@ -1206,46 +1204,46 @@ BaseContent::cEXC( const OUString& aNewName )
FileContentIdentifier* pp = new FileContentIdentifier( aNewName ); FileContentIdentifier* pp = new FileContentIdentifier( aNewName );
m_xContentIdentifier.set( pp ); m_xContentIdentifier.set( pp );
ContentEventNotifier* p = nullptr; std::unique_ptr<ContentEventNotifier> p;
if( m_pContentEventListeners ) if( m_pContentEventListeners )
p = new ContentEventNotifier( m_pMyShell, p.reset( new ContentEventNotifier( m_pMyShell,
this, this,
m_xContentIdentifier, m_xContentIdentifier,
xOldRef, xOldRef,
m_pContentEventListeners->getElements() ); m_pContentEventListeners->getElements() ) );
return p; return p;
} }
ContentEventNotifier* std::unique_ptr<ContentEventNotifier>
BaseContent::cCEL() BaseContent::cCEL()
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
ContentEventNotifier* p = nullptr; std::unique_ptr<ContentEventNotifier> p;
if( m_pContentEventListeners ) if( m_pContentEventListeners )
p = new ContentEventNotifier( m_pMyShell, p.reset( new ContentEventNotifier( m_pMyShell,
this, this,
m_xContentIdentifier, m_xContentIdentifier,
m_pContentEventListeners->getElements() ); m_pContentEventListeners->getElements() ) );
return p; return p;
} }
PropertySetInfoChangeNotifier* std::unique_ptr<PropertySetInfoChangeNotifier>
BaseContent::cPSL() BaseContent::cPSL()
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
PropertySetInfoChangeNotifier* p = nullptr; std::unique_ptr<PropertySetInfoChangeNotifier> p;
if( m_pPropertySetInfoChangeListeners ) if( m_pPropertySetInfoChangeListeners )
p = new PropertySetInfoChangeNotifier( this, p.reset( new PropertySetInfoChangeNotifier( this,
m_pPropertySetInfoChangeListeners->getElements() ); m_pPropertySetInfoChangeListeners->getElements() ) );
return p; return p;
} }
PropertyChangeNotifier* std::unique_ptr<PropertyChangeNotifier>
BaseContent::cPCL() BaseContent::cPCL()
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
@@ -1255,7 +1253,7 @@ BaseContent::cPCL()
Sequence< OUString > seqNames = m_pPropertyListener->getContainedTypes(); Sequence< OUString > seqNames = m_pPropertyListener->getContainedTypes();
PropertyChangeNotifier* p = nullptr; std::unique_ptr<PropertyChangeNotifier> p;
sal_Int32 length = seqNames.getLength(); sal_Int32 length = seqNames.getLength();
@@ -1270,8 +1268,8 @@ BaseContent::cPCL()
(*listener)[seqNames[i]] = pContainer->getElements(); (*listener)[seqNames[i]] = pContainer->getElements();
} }
p = new PropertyChangeNotifier( this, p.reset( new PropertyChangeNotifier( this,
listener ); listener ) );
} }
return p; return p;

View File

@@ -210,11 +210,11 @@ namespace fileaccess {
// Notifier // Notifier
ContentEventNotifier* cDEL() override; std::unique_ptr<ContentEventNotifier> cDEL() override;
ContentEventNotifier* cEXC( const OUString& aNewName ) override; std::unique_ptr<ContentEventNotifier> cEXC( const OUString& aNewName ) override;
ContentEventNotifier* cCEL() override; std::unique_ptr<ContentEventNotifier> cCEL() override;
PropertySetInfoChangeNotifier* cPSL() override; std::unique_ptr<PropertySetInfoChangeNotifier> cPSL() override;
PropertyChangeNotifier* cPCL() override; std::unique_ptr<PropertyChangeNotifier> cPCL() override;
private: private:
// Data members // Data members

View File

@@ -101,12 +101,12 @@ namespace fileaccess {
{ {
public: public:
// Side effect of this function is the change of the name // Side effect of this function is the change of the name
virtual ContentEventNotifier* cEXC( const OUString& aNewName ) = 0; virtual std::unique_ptr<ContentEventNotifier> cEXC( const OUString& aNewName ) = 0;
// Side effect is the change of the state of the object to "deleted". // Side effect is the change of the state of the object to "deleted".
virtual ContentEventNotifier* cDEL() = 0; virtual std::unique_ptr<ContentEventNotifier> cDEL() = 0;
virtual ContentEventNotifier* cCEL() = 0; virtual std::unique_ptr<ContentEventNotifier> cCEL() = 0;
virtual PropertySetInfoChangeNotifier* cPSL() = 0; virtual std::unique_ptr<PropertySetInfoChangeNotifier> cPSL() = 0;
virtual PropertyChangeNotifier* cPCL() = 0; virtual std::unique_ptr<PropertyChangeNotifier> cPCL() = 0;
protected: protected:
~Notifier() {} ~Notifier() {}

View File

@@ -61,27 +61,27 @@ class XResultSet_impl : public Notifier,
virtual ~XResultSet_impl() override; virtual ~XResultSet_impl() override;
virtual ContentEventNotifier* cDEL() override virtual std::unique_ptr<ContentEventNotifier> cDEL() override
{ {
return nullptr; return nullptr;
} }
virtual ContentEventNotifier* cEXC( const OUString& ) override virtual std::unique_ptr<ContentEventNotifier> cEXC( const OUString& ) override
{ {
return nullptr; return nullptr;
} }
virtual ContentEventNotifier* cCEL() override virtual std::unique_ptr<ContentEventNotifier> cCEL() override
{ {
return nullptr; return nullptr;
} }
virtual PropertySetInfoChangeNotifier* cPSL() override virtual std::unique_ptr<PropertySetInfoChangeNotifier> cPSL() override
{ {
return nullptr; return nullptr;
} }
virtual PropertyChangeNotifier* cPCL() override virtual std::unique_ptr<PropertyChangeNotifier> cPCL() override
{ {
return nullptr; return nullptr;
} }

View File

@@ -2604,11 +2604,10 @@ TaskManager::getv(
// EventListener // EventListener
std::vector< ContentEventNotifier* >* std::vector< std::unique_ptr< ContentEventNotifier > >
TaskManager::getContentEventListeners( const OUString& aName ) TaskManager::getContentEventListeners( const OUString& aName )
{ {
std::vector< ContentEventNotifier* >* p = new std::vector< ContentEventNotifier* >; std::vector< std::unique_ptr<ContentEventNotifier> > listeners;
std::vector< ContentEventNotifier* >& listeners = *p;
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
TaskManager::ContentMap::iterator it = m_aContent.find( aName ); TaskManager::ContentMap::iterator it = m_aContent.find( aName );
@@ -2617,21 +2616,20 @@ TaskManager::getContentEventListeners( const OUString& aName )
std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier ); std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier );
for (auto const& pointer : listOfNotifiers) for (auto const& pointer : listOfNotifiers)
{ {
ContentEventNotifier* notifier = pointer->cCEL(); std::unique_ptr<ContentEventNotifier> notifier = pointer->cCEL();
if( notifier ) if( notifier )
listeners.push_back( notifier ); listeners.push_back( std::move(notifier) );
} }
} }
} }
return p; return listeners;
} }
std::vector< ContentEventNotifier* >* std::vector< std::unique_ptr<ContentEventNotifier> >
TaskManager::getContentDeletedEventListeners( const OUString& aName ) TaskManager::getContentDeletedEventListeners( const OUString& aName )
{ {
std::vector< ContentEventNotifier* >* p = new std::vector< ContentEventNotifier* >; std::vector< std::unique_ptr< ContentEventNotifier > > listeners;
std::vector< ContentEventNotifier* >& listeners = *p;
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
TaskManager::ContentMap::iterator it = m_aContent.find( aName ); TaskManager::ContentMap::iterator it = m_aContent.find( aName );
@@ -2640,64 +2638,51 @@ TaskManager::getContentDeletedEventListeners( const OUString& aName )
std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier ); std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier );
for (auto const& pointer : listOfNotifiers) for (auto const& pointer : listOfNotifiers)
{ {
ContentEventNotifier* notifier = pointer->cDEL(); std::unique_ptr<ContentEventNotifier> notifier = pointer->cDEL();
if( notifier ) if( notifier )
listeners.push_back( notifier ); listeners.push_back( std::move(notifier) );
} }
} }
} }
return p; return listeners;
} }
void void
TaskManager::notifyInsert( std::vector< ContentEventNotifier* >* listeners,const OUString& aChildName ) TaskManager::notifyInsert( std::vector< std::unique_ptr<ContentEventNotifier> > listeners, const OUString& aChildName )
{ {
std::vector< ContentEventNotifier* >::iterator it = listeners->begin(); for (const auto & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyChildInserted( aChildName ); l->notifyChildInserted( aChildName );
delete *it;
++it;
} }
delete listeners;
} }
void void
TaskManager::notifyContentDeleted( std::vector< ContentEventNotifier* >* listeners ) TaskManager::notifyContentDeleted( std::vector< std::unique_ptr< ContentEventNotifier> > listeners )
{ {
std::vector< ContentEventNotifier* >::iterator it = listeners->begin(); for( auto const & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyDeleted(); l->notifyDeleted();
delete *it;
++it;
} }
delete listeners;
} }
void void
TaskManager::notifyContentRemoved( std::vector< ContentEventNotifier* >* listeners, TaskManager::notifyContentRemoved( std::vector< std::unique_ptr<ContentEventNotifier> > listeners,
const OUString& aChildName ) const OUString& aChildName )
{ {
std::vector< ContentEventNotifier* >::iterator it = listeners->begin(); for( auto const & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyRemoved( aChildName ); l->notifyRemoved( aChildName );
delete *it;
++it;
} }
delete listeners;
} }
std::vector< PropertySetInfoChangeNotifier* >* std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > >
TaskManager::getPropertySetListeners( const OUString& aName ) TaskManager::getPropertySetListeners( const OUString& aName )
{ {
std::vector< PropertySetInfoChangeNotifier* >* p = new std::vector< PropertySetInfoChangeNotifier* >; std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > > listeners;
std::vector< PropertySetInfoChangeNotifier* >& listeners = *p;
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
TaskManager::ContentMap::iterator it = m_aContent.find( aName ); TaskManager::ContentMap::iterator it = m_aContent.find( aName );
@@ -2706,55 +2691,44 @@ TaskManager::getPropertySetListeners( const OUString& aName )
std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier ); std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier );
for (auto const& pointer : listOfNotifiers) for (auto const& pointer : listOfNotifiers)
{ {
PropertySetInfoChangeNotifier* notifier = pointer->cPSL(); std::unique_ptr<PropertySetInfoChangeNotifier> notifier = pointer->cPSL();
if( notifier ) if( notifier )
listeners.push_back( notifier ); listeners.push_back( std::move(notifier) );
} }
} }
} }
return p; return listeners;
} }
void void
TaskManager::notifyPropertyAdded( std::vector< PropertySetInfoChangeNotifier* >* listeners, TaskManager::notifyPropertyAdded( std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > > listeners,
const OUString& aPropertyName ) const OUString& aPropertyName )
{ {
std::vector< PropertySetInfoChangeNotifier* >::iterator it = listeners->begin(); for( auto const & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyPropertyAdded( aPropertyName ); l->notifyPropertyAdded( aPropertyName );
delete *it;
++it;
} }
delete listeners;
} }
void void
TaskManager::notifyPropertyRemoved( std::vector< PropertySetInfoChangeNotifier* >* listeners, TaskManager::notifyPropertyRemoved( std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > > listeners,
const OUString& aPropertyName ) const OUString& aPropertyName )
{ {
std::vector< PropertySetInfoChangeNotifier* >::iterator it = listeners->begin(); for( auto const & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyPropertyRemoved( aPropertyName ); l->notifyPropertyRemoved( aPropertyName );
delete *it;
++it;
} }
delete listeners;
} }
std::vector< std::vector< ContentEventNotifier* >* >* std::vector< std::unique_ptr< ContentEventNotifier > >
TaskManager::getContentExchangedEventListeners( const OUString& aOldPrefix, TaskManager::getContentExchangedEventListeners( const OUString& aOldPrefix,
const OUString& aNewPrefix, const OUString& aNewPrefix,
bool withChildren ) bool withChildren )
{ {
std::vector< std::unique_ptr< ContentEventNotifier > > aVector;
std::vector< std::vector< ContentEventNotifier* >* >* aVectorOnHeap =
new std::vector< std::vector< ContentEventNotifier* >* >;
std::vector< std::vector< ContentEventNotifier* >* >& aVector = *aVectorOnHeap;
sal_Int32 count; sal_Int32 count;
OUString aOldName; OUString aOldName;
@@ -2785,9 +2759,6 @@ TaskManager::getContentExchangedEventListeners( const OUString& aOldPrefix,
for( sal_Int32 j = 0; j < count; ++j ) for( sal_Int32 j = 0; j < count; ++j )
{ {
std::vector< ContentEventNotifier* >* p = new std::vector< ContentEventNotifier* >;
std::vector< ContentEventNotifier* >& listeners = *p;
if( withChildren ) if( withChildren )
{ {
aOldName = oldChildList[j]; aOldName = oldChildList[j];
@@ -2817,9 +2788,9 @@ TaskManager::getContentExchangedEventListeners( const OUString& aOldPrefix,
std::vector<Notifier*>& listOfNotifiers = *( itnew->second.notifier ); std::vector<Notifier*>& listOfNotifiers = *( itnew->second.notifier );
for (auto const& pointer : listOfNotifiers) for (auto const& pointer : listOfNotifiers)
{ {
ContentEventNotifier* notifier = pointer->cEXC( aNewName ); std::unique_ptr<ContentEventNotifier> notifier = pointer->cEXC( aNewName );
if( notifier ) if( notifier )
listeners.push_back( notifier ); aVector.push_back( std::move(notifier) );
} }
} }
@@ -2836,37 +2807,27 @@ TaskManager::getContentExchangedEventListeners( const OUString& aOldPrefix,
} }
delete copyList; delete copyList;
} }
aVector.push_back( p );
} }
} }
return aVectorOnHeap; return aVector;
} }
void void
TaskManager::notifyContentExchanged( std::vector< std::vector< ContentEventNotifier* >* >* listeners_vec ) TaskManager::notifyContentExchanged( std::vector< std::unique_ptr< ContentEventNotifier > > listeners_vec )
{ {
for( std::vector< ContentEventNotifier* >* listeners : *listeners_vec) for( auto & l : listeners_vec)
{ {
std::vector< ContentEventNotifier* >::iterator it = listeners->begin(); l->notifyExchanged();
while( it != listeners->end() )
{
(*it)->notifyExchanged();
delete *it;
++it;
}
delete listeners;
} }
delete listeners_vec;
} }
std::vector< PropertyChangeNotifier* >* std::vector< std::unique_ptr<PropertyChangeNotifier> >
TaskManager::getPropertyChangeNotifier( const OUString& aName ) TaskManager::getPropertyChangeNotifier( const OUString& aName )
{ {
std::vector< PropertyChangeNotifier* >* p = new std::vector< PropertyChangeNotifier* >; std::vector< std::unique_ptr<PropertyChangeNotifier> > listeners;
std::vector< PropertyChangeNotifier* >& listeners = *p;
{ {
osl::MutexGuard aGuard( m_aMutex ); osl::MutexGuard aGuard( m_aMutex );
TaskManager::ContentMap::iterator it = m_aContent.find( aName ); TaskManager::ContentMap::iterator it = m_aContent.find( aName );
@@ -2875,27 +2836,23 @@ TaskManager::getPropertyChangeNotifier( const OUString& aName )
std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier ); std::vector<Notifier*>& listOfNotifiers = *( it->second.notifier );
for (auto const& pointer : listOfNotifiers) for (auto const& pointer : listOfNotifiers)
{ {
PropertyChangeNotifier* notifier = pointer->cPCL(); std::unique_ptr<PropertyChangeNotifier> notifier = pointer->cPCL();
if( notifier ) if( notifier )
listeners.push_back( notifier ); listeners.push_back( std::move(notifier) );
} }
} }
} }
return p; return listeners;
} }
void TaskManager::notifyPropertyChanges( std::vector< PropertyChangeNotifier* >* listeners, void TaskManager::notifyPropertyChanges( std::vector< std::unique_ptr< PropertyChangeNotifier > > listeners,
const uno::Sequence< beans::PropertyChangeEvent >& seqChanged ) const uno::Sequence< beans::PropertyChangeEvent >& seqChanged )
{ {
std::vector< PropertyChangeNotifier* >::iterator it = listeners->begin(); for( auto const & l : listeners )
while( it != listeners->end() )
{ {
(*it)->notifyPropertyChanged( seqChanged ); l->notifyPropertyChanged( seqChanged );
delete *it;
++it;
} }
delete listeners;
} }

View File

@@ -515,21 +515,21 @@ namespace fileaccess
/* get eventListeners */ /* get eventListeners */
/********************************************************************************/ /********************************************************************************/
std::vector< ContentEventNotifier* >* std::vector< std::unique_ptr< ContentEventNotifier > >
getContentEventListeners( const OUString& aName ); getContentEventListeners( const OUString& aName );
std::vector< ContentEventNotifier* >* std::vector< std::unique_ptr< ContentEventNotifier > >
getContentDeletedEventListeners( const OUString& aName ); getContentDeletedEventListeners( const OUString& aName );
std::vector< std::vector< ContentEventNotifier* >* >* std::vector< std::unique_ptr < ContentEventNotifier > >
getContentExchangedEventListeners( const OUString& aOldPrefix, getContentExchangedEventListeners( const OUString& aOldPrefix,
const OUString& aNewPrefix, const OUString& aNewPrefix,
bool withChildren ); bool withChildren );
std::vector< PropertyChangeNotifier* >* std::vector< std::unique_ptr< PropertyChangeNotifier > >
getPropertyChangeNotifier( const OUString& aName ); getPropertyChangeNotifier( const OUString& aName );
std::vector< PropertySetInfoChangeNotifier* >* std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > >
getPropertySetListeners( const OUString& aName ); getPropertySetListeners( const OUString& aName );
@@ -538,28 +538,28 @@ namespace fileaccess
/********************************************************************************/ /********************************************************************************/
static void notifyPropertyChanges( static void notifyPropertyChanges(
std::vector< PropertyChangeNotifier* >* listeners, std::vector< std::unique_ptr< PropertyChangeNotifier > > listeners,
const css::uno::Sequence< css::beans::PropertyChangeEvent >& seqChanged ); const css::uno::Sequence< css::beans::PropertyChangeEvent >& seqChanged );
static void notifyContentExchanged( static void notifyContentExchanged(
std::vector< std::vector< ContentEventNotifier* >* >* listeners_vec ); std::vector< std::unique_ptr< ContentEventNotifier > > listeners_vec );
static void notifyInsert( static void notifyInsert(
std::vector< ContentEventNotifier* >* listeners,const OUString& aChildName ); std::vector< std::unique_ptr< ContentEventNotifier > > listeners, const OUString& aChildName );
static void notifyContentDeleted( static void notifyContentDeleted(
std::vector< ContentEventNotifier* >* listeners ); std::vector< std::unique_ptr< ContentEventNotifier > > listeners );
static void notifyContentRemoved( static void notifyContentRemoved(
std::vector< ContentEventNotifier* >* listeners, std::vector< std::unique_ptr< ContentEventNotifier > > listeners,
const OUString& aChildName ); const OUString& aChildName );
static void notifyPropertyAdded( static void notifyPropertyAdded(
std::vector< PropertySetInfoChangeNotifier* >* listeners, std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > > listeners,
const OUString& aPropertyName ); const OUString& aPropertyName );
static void notifyPropertyRemoved( static void notifyPropertyRemoved(
std::vector< PropertySetInfoChangeNotifier* >* listeners, std::vector< std::unique_ptr< PropertySetInfoChangeNotifier > > listeners,
const OUString& aPropertyName ); const OUString& aPropertyName );