Skip to content

Commit 3dc832a

Browse files
committedDec 22, 2015
Don't crash providers when destrucing with fake open connection
1 parent 73ba0e8 commit 3dc832a

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed
 

‎src/core/qgsfeatureiterator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class QgsExpressionSorter
120120
QgsAbstractFeatureIterator::QgsAbstractFeatureIterator( const QgsFeatureRequest& request )
121121
: mRequest( request )
122122
, mClosed( false )
123+
, mZombie( false )
123124
, refs( 0 )
124125
, mFetchedCount( 0 )
125126
, mGeometrySimplifier( nullptr )
@@ -152,8 +153,8 @@ bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )
152153
else
153154
{
154155
dataOk = false;
155-
// don't call close, the provider connection has already been closed
156-
mClosed = true;
156+
// even the zombie dies at this point...
157+
mZombie = false;
157158
}
158159
}
159160
else
@@ -289,7 +290,8 @@ void QgsAbstractFeatureIterator::setupOrderBy( const QList<QgsFeatureRequest::Or
289290

290291
mFeatureIterator = mCachedFeatures.constBegin();
291292
mUseCachedFeatures = true;
292-
mClosed = false;
293+
// The real iterator is closed, we are only serving cached features
294+
mZombie = true;
293295
}
294296
}
295297

‎src/core/qgsfeatureiterator.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ class CORE_EXPORT QgsAbstractFeatureIterator
8989
/** Set to true, as soon as the iterator is closed. */
9090
bool mClosed;
9191

92+
/**
93+
* A feature iterator may be closed already but still be serving features from the cache.
94+
* This is done when we serve features which have been pre-fetched and the order by has
95+
* been locally sorted.
96+
* In such a scenario, all resources have been released (mClosed is true) but the deads
97+
* are still alive.
98+
*/
99+
bool mZombie;
100+
92101
//! reference counting (to allow seamless copying of QgsFeatureIterator instances)
102+
//! TODO QGIS3: make this private
93103
int refs;
94104
void ref(); //!< add reference
95105
void deref(); //!< remove reference, delete if refs == 0
@@ -247,7 +257,7 @@ inline bool QgsFeatureIterator::close()
247257

248258
inline bool QgsFeatureIterator::isClosed() const
249259
{
250-
return mIter ? mIter->mClosed : true;
260+
return mIter ? mIter->mClosed && !mIter->mZombie : true;
251261
}
252262

253263
inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )

‎src/providers/ogr/qgsogrfeatureiterator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ bool QgsOgrFeatureIterator::rewind()
243243

244244
bool QgsOgrFeatureIterator::close()
245245
{
246-
if ( mClosed )
246+
if ( !mConn )
247247
return false;
248248

249249
iteratorClosed();
@@ -253,7 +253,9 @@ bool QgsOgrFeatureIterator::close()
253253
OGR_DS_ReleaseResultSet( mConn->ds, ogrLayer );
254254
}
255255

256-
QgsOgrConnPool::instance()->releaseConnection( mConn );
256+
if ( mConn )
257+
QgsOgrConnPool::instance()->releaseConnection( mConn );
258+
257259
mConn = nullptr;
258260

259261
mClosed = true;

‎src/providers/postgres/qgspostgresfeatureiterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ bool QgsPostgresFeatureIterator::rewind()
313313

314314
bool QgsPostgresFeatureIterator::close()
315315
{
316-
if ( mClosed )
316+
if ( !mConn )
317317
return false;
318318

319319
mConn->closeCursor( mCursorName );

‎src/providers/spatialite/qgsspatialitefeatureiterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ bool QgsSpatiaLiteFeatureIterator::rewind()
235235

236236
bool QgsSpatiaLiteFeatureIterator::close()
237237
{
238-
if ( mClosed )
238+
if ( !mHandle )
239239
return false;
240240

241241
iteratorClosed();

0 commit comments

Comments
 (0)
Please sign in to comment.