Skip to content

Commit

Permalink
Avoid insertion of null pointers in layer registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Dec 8, 2015
1 parent 269f709 commit ec20db7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -119,7 +119,7 @@ void QgsMapLayerRegistry::removeMapLayers( const QStringList& theLayerIds )
QList<QgsMapLayer*> layers; QList<QgsMapLayer*> layers;
Q_FOREACH ( const QString &myId, theLayerIds ) Q_FOREACH ( const QString &myId, theLayerIds )
{ {
layers << mMapLayers[myId]; layers << mMapLayers.value( myId );
} }


removeMapLayers( layers ); removeMapLayers( layers );
Expand Down Expand Up @@ -160,12 +160,13 @@ void QgsMapLayerRegistry::removeMapLayers( const QList<QgsMapLayer*>& layers )


void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId ) void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
{ {
removeMapLayers( QList<QgsMapLayer*>() << mMapLayers[theLayerId] ); removeMapLayers( QList<QgsMapLayer*>() << mMapLayers.value( theLayerId ) );
} }


void QgsMapLayerRegistry::removeMapLayer( QgsMapLayer* layer ) void QgsMapLayerRegistry::removeMapLayer( QgsMapLayer* layer )
{ {
removeMapLayers( QList<QgsMapLayer*>() << layer ); if ( layer )
removeMapLayers( QList<QgsMapLayer*>() << layer );
} }


void QgsMapLayerRegistry::removeAllMapLayers() void QgsMapLayerRegistry::removeAllMapLayers()
Expand Down
5 changes: 5 additions & 0 deletions tests/src/python/test_qgsmaplayerregistry.py
Expand Up @@ -25,6 +25,11 @@ def test_RemoveLayerShouldNotSegFault(self):
reg = QgsMapLayerRegistry.instance() reg = QgsMapLayerRegistry.instance()
# Should not segfault # Should not segfault
reg.removeMapLayers(['not_exists']) reg.removeMapLayers(['not_exists'])
reg.removeMapLayer('not_exists2')

# check also that the removal of an unexistant layer does not insert a null layer
for k, layer in reg.mapLayers().items():
assert(layer is not None)




if __name__ == '__main__': if __name__ == '__main__':
Expand Down

0 comments on commit ec20db7

Please sign in to comment.