Skip to content

Commit f04ea99

Browse files
committed
[layertree] Fix possible crash when removing layers
1 parent df85395 commit f04ea99

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/core/layertree/qgslayertreeregistrybridge.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root, QObject *parent )
2626
: QObject( parent )
2727
, mRoot( root )
28+
, mRegistryRemovingLayers( false )
2829
, mEnabled( true )
2930
, mNewLayersVisible( true )
3031
, mInsertionPointGroup( root )
@@ -74,12 +75,18 @@ void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
7475
if ( !mEnabled )
7576
return;
7677

78+
// when we start removing child nodes, the bridge would try to remove those layers from
79+
// the registry _again_ in groupRemovedChildren() - this prevents it
80+
mRegistryRemovingLayers = true;
81+
7782
foreach ( QString layerId, layerIds )
7883
{
7984
QgsLayerTreeLayer* nodeLayer = mRoot->findLayer( layerId );
8085
if ( nodeLayer )
8186
qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent() )->removeChildNode( nodeLayer );
8287
}
88+
89+
mRegistryRemovingLayers = false;
8390
}
8491

8592

@@ -101,6 +108,9 @@ static void _collectLayerIdsInGroup( QgsLayerTreeGroup* group, int indexFrom, in
101108

102109
void QgsLayerTreeRegistryBridge::groupWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo )
103110
{
111+
if ( mRegistryRemovingLayers )
112+
return; // do not try to remove those layers again
113+
104114
Q_ASSERT( mLayerIdsForRemoval.isEmpty() );
105115

106116
Q_ASSERT( QgsLayerTree::isGroup( node ) );
@@ -111,6 +121,9 @@ void QgsLayerTreeRegistryBridge::groupWillRemoveChildren( QgsLayerTreeNode* node
111121

112122
void QgsLayerTreeRegistryBridge::groupRemovedChildren()
113123
{
124+
if ( mRegistryRemovingLayers )
125+
return; // do not try to remove those layers again
126+
114127
// remove only those that really do not exist in the tree
115128
// (ignores layers that were dragged'n'dropped: 1. drop new 2. remove old)
116129
QStringList toRemove;

src/core/layertree/qgslayertreeregistrybridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
6464
protected:
6565
QgsLayerTreeGroup* mRoot;
6666
QStringList mLayerIdsForRemoval;
67+
bool mRegistryRemovingLayers;
6768
bool mEnabled;
6869
bool mNewLayersVisible;
6970

0 commit comments

Comments
 (0)