Skip to content

Commit f3b5838

Browse files
committedMar 16, 2018
[ogr] Fix ref/unref mismatch when loading OGR layers
Causes an extra connection reference which is never removed, blocking ogr dataset closing. Fixes #18420, probably others
1 parent bf12404 commit f3b5838

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
 

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ bool QgsOgrProvider::commitTransaction()
16861686
return true;
16871687
}
16881688

1689-
bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeatureCount, bool updateCapabilities )
1689+
bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeatureCount, bool updateCapabilities, bool hasExistingRef )
16901690
{
16911691
QgsCPLErrorHandler handler;
16921692

@@ -1745,9 +1745,11 @@ bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeature
17451745

17461746
if ( uri != dataSourceUri() )
17471747
{
1748-
QgsOgrConnPool::instance()->unref( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ) ) );
1748+
if ( hasExistingRef )
1749+
QgsOgrConnPool::instance()->unref( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ) ) );
17491750
setDataSourceUri( uri );
1750-
QgsOgrConnPool::instance()->ref( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ) ) );
1751+
if ( hasExistingRef )
1752+
QgsOgrConnPool::instance()->ref( QgsOgrProviderUtils::connectionPoolId( dataSourceUri( true ) ) );
17511753
}
17521754

17531755
mOgrLayer->ResetReading();
@@ -3994,8 +3996,13 @@ void QgsOgrProvider::open( OpenMode mode )
39943996
mSubsetString.clear();
39953997
// Block signals to avoid endless recursion reloadData -> emit dataChanged -> reloadData
39963998
blockSignals( true );
3999+
39974000
// Do not update capabilities: it will be done later
3998-
mValid = _setSubsetString( origSubsetString, true, false );
4001+
4002+
// WARNING if this is the initial open - we don't already have a connection ref, and will be creating one later. So we *mustn't* grab an extra connection ref
4003+
// while setting the subset string, or we'll be left with an extra reference which is never cleared.
4004+
mValid = _setSubsetString( origSubsetString, true, false, mode != OpenModeInitial );
4005+
39994006
blockSignals( false );
40004007
if ( mValid )
40014008
{

‎src/providers/ogr/qgsogrprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
211211
bool commitTransaction();
212212

213213
//! Does the real job of settings the subset string and adds an argument to disable update capabilities
214-
bool _setSubsetString( const QString &theSQL, bool updateFeatureCount = true, bool updateCapabilities = true );
214+
bool _setSubsetString( const QString &theSQL, bool updateFeatureCount = true, bool updateCapabilities = true, bool hasExistingRef = true );
215215

216216
void addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer ) const;
217217

0 commit comments

Comments
 (0)
Please sign in to comment.