Skip to content

Commit 9aea6ad

Browse files
committedMar 27, 2013
Fix issue #7379: Crash when attribute table is closed after layer has been deleted
1 parent 24f1519 commit 9aea6ad

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed
 

‎src/core/qgsvectorlayercache.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ QgsVectorLayerCache::QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize,
2727

2828
connect( mLayer, SIGNAL( featureDeleted( QgsFeatureId ) ), SLOT( featureDeleted( QgsFeatureId ) ) );
2929
connect( mLayer, SIGNAL( featureAdded( QgsFeatureId ) ), SLOT( featureAdded( QgsFeatureId ) ) );
30+
connect( mLayer, SIGNAL( layerDeleted() ), SLOT( layerDeleted() ) );
3031

3132
setCacheGeometry( true );
3233
setCacheSubsetOfAttributes( mLayer->pendingAllAttributesList() );
@@ -228,6 +229,13 @@ void QgsVectorLayerCache::geometryChanged( QgsFeatureId fid, QgsGeometry& geom )
228229
}
229230
}
230231

232+
void QgsVectorLayerCache::layerDeleted()
233+
{
234+
emit ( cachedLayerDeleted() );
235+
236+
mLayer = NULL;
237+
}
238+
231239
QgsFeatureIterator QgsVectorLayerCache::getFeatures( const QgsFeatureRequest &featureRequest )
232240
{
233241
QgsFeatureIterator it;

‎src/core/qgsvectorlayercache.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,21 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
209209
*/
210210
void progress( int i, bool& cancel );
211211

212+
/**
213+
* @brief Is emitted when the cached layer is deleted. Is emitted when the cached layers layerDelete()
214+
* signal is being emitted, but before the local reference to it has been set to NULL. So call to
215+
* @link {layer()} will still return a valid pointer for cleanup purpose.
216+
*/
217+
void cachedLayerDeleted();
218+
212219
public slots:
213220
void attributeValueChanged( QgsFeatureId fid, int field, const QVariant& value );
214221
void featureDeleted( QgsFeatureId fid );
215222
void featureAdded( QgsFeatureId fid );
216223
void attributeAdded( int field );
217224
void attributeDeleted( int field );
218225
void geometryChanged( QgsFeatureId fid, QgsGeometry& geom );
226+
void layerDeleted();
219227

220228
private:
221229

‎src/gui/attributetable/qgsattributetablefiltermodel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ void QgsAttributeTableFilterModel::announcedInvalidateFilter()
241241

242242
void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
243243
{
244+
if ( !layer() )
245+
return;
246+
244247
bool filter = false;
245248
QgsRectangle rect = mCanvas->mapRenderer()->mapToLayerCoordinates( layer(), mCanvas->extent() );
246249
QgsRenderContext renderContext;

‎src/gui/attributetable/qgsattributetablemodel.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,21 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
4545
connect( layer(), SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ) );
4646
connect( layer(), SIGNAL( attributeAdded( int ) ), this, SLOT( attributeAdded( int ) ) );
4747
connect( layer(), SIGNAL( attributeDeleted( int ) ), this, SLOT( attributeDeleted( int ) ) );
48+
connect( mLayerCache, SIGNAL( cachedLayerDeleted() ), this, SLOT( layerDeleted() ) );
4849
}
4950

5051
QgsAttributeTableModel::~QgsAttributeTableModel()
5152
{
52-
const QgsFields& fields = layer()->pendingFields();
53-
for ( int idx = 0; idx < fields.count(); ++idx )
53+
if ( layer() )
5454
{
55-
if ( layer()->editType( idx ) != QgsVectorLayer::ValueRelation )
56-
continue;
55+
const QgsFields& fields = layer()->pendingFields();
56+
for ( int idx = 0; idx < fields.count(); ++idx )
57+
{
58+
if ( layer()->editType( idx ) != QgsVectorLayer::ValueRelation )
59+
continue;
5760

58-
delete mValueMaps.take( idx );
61+
delete mValueMaps.take( idx );
62+
}
5963
}
6064
}
6165

@@ -163,6 +167,15 @@ void QgsAttributeTableModel::layerDeleted()
163167
beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
164168
removeRows( 0, rowCount() );
165169
endRemoveRows();
170+
171+
const QgsFields& fields = layer()->pendingFields();
172+
for ( int idx = 0; idx < fields.count(); ++idx )
173+
{
174+
if ( layer()->editType( idx ) != QgsVectorLayer::ValueRelation )
175+
continue;
176+
177+
delete mValueMaps.take( idx );
178+
}
166179
}
167180

168181
void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
@@ -393,6 +406,9 @@ int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
393406

394407
QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
395408
{
409+
if ( !layer() )
410+
return QVariant();
411+
396412
if ( role == Qt::DisplayRole )
397413
{
398414
if ( orientation == Qt::Vertical ) //row

0 commit comments

Comments
 (0)
Please sign in to comment.