Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Safety checks for unregistering map layers from registry
If a map layer which is registered is deleted outside of the layer
registry but not unregistered, the layer registry would still happily
return a pointer to this layer if queried with its id.

Up to now, this caused crashes. Now, the layer will be unregistered and
a warning is printed.

This patch also contains slight improvements to other parts of the map
layer registry.
  • Loading branch information
m-kuhn committed Jul 11, 2016
1 parent c1e1b28 commit ddf98bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -73,12 +73,11 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
bool takeOwnership )
{
QList<QgsMapLayer *> myResultList;
for ( int i = 0; i < theMapLayers.size(); ++i )
Q_FOREACH ( QgsMapLayer* myLayer, theMapLayers )
{
QgsMapLayer * myLayer = theMapLayers.at( i );
if ( !myLayer || !myLayer->isValid() )
{
QgsDebugMsg( "cannot add invalid layers" );
QgsDebugMsg( "Cannot add invalid layers" );
continue;
}
//check the layer is not already registered!
Expand All @@ -87,7 +86,10 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
mMapLayers[myLayer->id()] = myLayer;
myResultList << mMapLayers[myLayer->id()];
if ( takeOwnership )
mOwnedLayers << myLayer;
{
myLayer->setParent( this );
}
connect( myLayer, SIGNAL( destroyed( QObject* ) ), this, SLOT( onMapLayerDeleted( QObject* ) ) );
emit layerWasAdded( myLayer );
}
}
Expand Down Expand Up @@ -151,12 +153,11 @@ void QgsMapLayerRegistry::removeMapLayers( const QList<QgsMapLayer*>& layers )
QString myId( lyr->id() );
emit layerWillBeRemoved( myId );
emit layerWillBeRemoved( lyr );
if ( mOwnedLayers.contains( lyr ) )
mMapLayers.remove( myId );
if ( lyr->parent() == this )
{
delete lyr;
mOwnedLayers.remove( lyr );
}
mMapLayers.remove( myId );
emit layerRemoved( myId );
}

Expand Down Expand Up @@ -185,14 +186,20 @@ void QgsMapLayerRegistry::removeAllMapLayers()

void QgsMapLayerRegistry::reloadAllLayers()
{
QMap<QString, QgsMapLayer *>::iterator it;
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
Q_FOREACH ( QgsMapLayer* layer, mMapLayers )
{
QgsMapLayer* layer = it.value();
if ( layer )
{
layer->reload();
}
layer->reload();
}
}

void QgsMapLayerRegistry::onMapLayerDeleted( QObject* obj )
{
QString id = mMapLayers.key( static_cast<QgsMapLayer*>( obj ) );

if ( !id.isNull() )
{
QgsDebugMsg( QString( "Map layer deleted without unregistering! %1" ).arg( id ) );
mMapLayers.remove( id );
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsmaplayerregistry.h
Expand Up @@ -283,12 +283,14 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
void connectNotify( const char * signal ) override;
#endif

private slots:
void onMapLayerDeleted( QObject* obj );

private:
//! private singleton constructor
QgsMapLayerRegistry( QObject * parent = nullptr );

QMap<QString, QgsMapLayer*> mMapLayers;
QSet<QgsMapLayer*> mOwnedLayers;
}; // class QgsMapLayerRegistry

#endif //QgsMapLayerRegistry_H
Expand Down

0 comments on commit ddf98bc

Please sign in to comment.