Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix leaked GDAL dataset handle when deleting layers
Prevents the deletion from actually occurring until QGIS is closed

(cherry picked from commit 842485f)
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Jul 30, 2021
1 parent 521df10 commit 55e80a7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -7073,16 +7073,16 @@ bool QgsOgrProviderUtils::deleteLayer( const QString &uri, QString &errCause )
openOptions );


GDALDatasetH hDS = GDALOpenEx( filePath.toUtf8().constData(), GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr );
gdal::dataset_unique_ptr hDS( GDALOpenEx( filePath.toUtf8().constData(), GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr ) );
if ( hDS && ( ! layerName.isEmpty() || layerIndex != -1 ) )
{
// If we have got a name we convert it into an index
if ( ! layerName.isEmpty() )
{
layerIndex = -1;
for ( int i = 0; i < GDALDatasetGetLayerCount( hDS ); i++ )
for ( int i = 0; i < GDALDatasetGetLayerCount( hDS.get() ); i++ )
{
OGRLayerH hL = GDALDatasetGetLayer( hDS, i );
OGRLayerH hL = GDALDatasetGetLayer( hDS.get(), i );
if ( layerName == QString::fromUtf8( OGR_L_GetName( hL ) ) )
{
layerIndex = i;
Expand All @@ -7093,7 +7093,7 @@ bool QgsOgrProviderUtils::deleteLayer( const QString &uri, QString &errCause )
// Do delete!
if ( layerIndex != -1 )
{
OGRErr error = GDALDatasetDeleteLayer( hDS, layerIndex );
OGRErr error = GDALDatasetDeleteLayer( hDS.get(), layerIndex );
switch ( error )
{
case OGRERR_NOT_ENOUGH_DATA:
Expand Down Expand Up @@ -7123,7 +7123,6 @@ bool QgsOgrProviderUtils::deleteLayer( const QString &uri, QString &errCause )
case OGRERR_NON_EXISTING_FEATURE:
errCause = QObject::tr( "Non existing feature" );
break;
default:
case OGRERR_NONE:
errCause = QObject::tr( "Success" );
break;
Expand Down

0 comments on commit 55e80a7

Please sign in to comment.