Skip to content

Commit 0f6256e

Browse files
author
Hugo Mercier
committed
Merge pull request #2556 from mhugo/fix_nullp
Avoid insertion of null pointers in layer registry
2 parents f13104b + ec20db7 commit 0f6256e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/core/qgsmaplayerregistry.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void QgsMapLayerRegistry::removeMapLayers( const QStringList& theLayerIds )
119119
QList<QgsMapLayer*> layers;
120120
Q_FOREACH ( const QString &myId, theLayerIds )
121121
{
122-
layers << mMapLayers[myId];
122+
layers << mMapLayers.value( myId );
123123
}
124124

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

161161
void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
162162
{
163-
removeMapLayers( QList<QgsMapLayer*>() << mMapLayers[theLayerId] );
163+
removeMapLayers( QList<QgsMapLayer*>() << mMapLayers.value( theLayerId ) );
164164
}
165165

166166
void QgsMapLayerRegistry::removeMapLayer( QgsMapLayer* layer )
167167
{
168-
removeMapLayers( QList<QgsMapLayer*>() << layer );
168+
if ( layer )
169+
removeMapLayers( QList<QgsMapLayer*>() << layer );
169170
}
170171

171172
void QgsMapLayerRegistry::removeAllMapLayers()

tests/src/python/test_qgsmaplayerregistry.py

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def test_RemoveLayerShouldNotSegFault(self):
2525
reg = QgsMapLayerRegistry.instance()
2626
# Should not segfault
2727
reg.removeMapLayers(['not_exists'])
28+
reg.removeMapLayer('not_exists2')
29+
30+
# check also that the removal of an unexistant layer does not insert a null layer
31+
for k, layer in reg.mapLayers().items():
32+
assert(layer is not None)
2833

2934

3035
if __name__ == '__main__':

0 commit comments

Comments
 (0)