Skip to content
Permalink
Browse files

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 ddf98bc13d28ba2abea75b5cb5a7753332f692b3
Showing with 24 additions and 15 deletions.
  1. +21 −14 src/core/qgsmaplayerregistry.cpp
  2. +3 −1 src/core/qgsmaplayerregistry.h
@@ -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!
@@ -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 );
}
}
@@ -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 );
}

@@ -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 );
}
}

@@ -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

0 comments on commit ddf98bc

Please sign in to comment.
You can’t perform that action at this time.