@@ -1323,9 +1323,6 @@ bool QgsOgrProvider::changeGeometryValues( const QgsGeometryMap &geometry_map )
1323
1323
if ( !doInitialActionsForEdition () )
1324
1324
return false ;
1325
1325
1326
- OGRFeatureH theOGRFeature = nullptr ;
1327
- OGRGeometryH theNewGeometry = nullptr ;
1328
-
1329
1326
setRelevantFields ( ogrLayer, true , attributeIndexes () );
1330
1327
1331
1328
for ( QgsGeometryMap::const_iterator it = geometry_map.constBegin (); it != geometry_map.constEnd (); ++it )
@@ -1336,46 +1333,55 @@ bool QgsOgrProvider::changeGeometryValues( const QgsGeometryMap &geometry_map )
1336
1333
continue ;
1337
1334
}
1338
1335
1339
- theOGRFeature = OGR_L_GetFeature ( ogrLayer, static_cast <long >( FID_TO_NUMBER ( it.key () ) ) );
1336
+ OGRFeatureH theOGRFeature = OGR_L_GetFeature ( ogrLayer, static_cast <long >( FID_TO_NUMBER ( it.key () ) ) );
1340
1337
if ( !theOGRFeature )
1341
1338
{
1342
1339
pushError ( tr ( " OGR error changing geometry: feature %1 not found" ).arg ( it.key () ) );
1343
1340
continue ;
1344
1341
}
1345
1342
1346
- // create an OGRGeometry
1347
- if ( OGR_G_CreateFromWkb ( const_cast <unsigned char *>( it->asWkb () ),
1348
- OGR_L_GetSpatialRef ( ogrLayer ),
1349
- &theNewGeometry,
1350
- it->wkbSize () ) != OGRERR_NONE )
1343
+ OGRGeometryH theNewGeometry = nullptr ;
1344
+ // We might receive null geometries. It is ok, but don't go through the
1345
+ // OGR_G_CreateFromWkb() route then
1346
+ if ( it->wkbSize () != 0 )
1351
1347
{
1352
- pushError ( tr ( " OGR error creating geometry for feature %1: %2" ).arg ( it.key () ).arg ( CPLGetLastErrorMsg () ) );
1353
- OGR_G_DestroyGeometry ( theNewGeometry );
1354
- theNewGeometry = nullptr ;
1355
- continue ;
1356
- }
1348
+ // create an OGRGeometry
1349
+ if ( OGR_G_CreateFromWkb ( const_cast <unsigned char *>( it->asWkb () ),
1350
+ OGR_L_GetSpatialRef ( ogrLayer ),
1351
+ &theNewGeometry,
1352
+ it->wkbSize () ) != OGRERR_NONE )
1353
+ {
1354
+ pushError ( tr ( " OGR error creating geometry for feature %1: %2" ).arg ( it.key () ).arg ( CPLGetLastErrorMsg () ) );
1355
+ OGR_G_DestroyGeometry ( theNewGeometry );
1356
+ theNewGeometry = nullptr ;
1357
+ OGR_F_Destroy ( theOGRFeature );
1358
+ continue ;
1359
+ }
1357
1360
1358
- if ( !theNewGeometry )
1359
- {
1360
- pushError ( tr ( " OGR error in feature %1: geometry is null" ).arg ( it.key () ) );
1361
- continue ;
1361
+ if ( !theNewGeometry )
1362
+ {
1363
+ pushError ( tr ( " OGR error in feature %1: geometry is null" ).arg ( it.key () ) );
1364
+ OGR_F_Destroy ( theOGRFeature );
1365
+ continue ;
1366
+ }
1362
1367
}
1363
1368
1364
1369
// set the new geometry
1365
1370
if ( OGR_F_SetGeometryDirectly ( theOGRFeature, theNewGeometry ) != OGRERR_NONE )
1366
1371
{
1367
1372
pushError ( tr ( " OGR error setting geometry of feature %1: %2" ).arg ( it.key () ).arg ( CPLGetLastErrorMsg () ) );
1368
- OGR_G_DestroyGeometry ( theNewGeometry );
1369
- theNewGeometry = nullptr ;
1373
+ // Shouldn't happen normally. If it happens, ownership of the geometry
1374
+ // may be not really well defined, so better not destroy it, but just
1375
+ // the feature.
1376
+ OGR_F_Destroy ( theOGRFeature );
1370
1377
continue ;
1371
1378
}
1372
1379
1373
1380
1374
1381
if ( OGR_L_SetFeature ( ogrLayer, theOGRFeature ) != OGRERR_NONE )
1375
1382
{
1376
1383
pushError ( tr ( " OGR error setting feature %1: %2" ).arg ( it.key () ).arg ( CPLGetLastErrorMsg () ) );
1377
- OGR_G_DestroyGeometry ( theNewGeometry );
1378
- theNewGeometry = nullptr ;
1384
+ OGR_F_Destroy ( theOGRFeature );
1379
1385
continue ;
1380
1386
}
1381
1387
mShapefileMayBeCorrupted = true ;
0 commit comments