Skip to content

Commit

Permalink
Merge pull request #44462 from qgis/backport-44449-to-queued_ltr_back…
Browse files Browse the repository at this point in the history
…ports

[Backport queued_ltr_backports] Fix leaked GDAL dataset handle when deleting layers (3.20)
  • Loading branch information
rouault committed Jul 31, 2021
2 parents 6ca5557 + 55e80a7 commit 13f5ad8
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 13f5ad8

Please sign in to comment.