Skip to content

Commit

Permalink
Fix large memory leak in vector file writer (fix #16003)
Browse files Browse the repository at this point in the history
(cherry-picked from 826a86)
  • Loading branch information
nyalldawson committed Feb 13, 2017
1 parent 34e307b commit 119e181
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
21 changes: 5 additions & 16 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -86,7 +86,6 @@ QgsVectorFileWriter::QgsVectorFileWriter(
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
, mGeom( nullptr )
, mError( NoError )
, mCodec( nullptr )
, mWkbType( QGis::fromOldWkbType( geometryType ) )
Expand All @@ -103,7 +102,6 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName, const Q
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
, mGeom( nullptr )
, mError( NoError )
, mCodec( nullptr )
, mWkbType( geometryType )
Expand Down Expand Up @@ -132,7 +130,6 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName,
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
, mGeom( nullptr )
, mError( NoError )
, mCodec( nullptr )
, mWkbType( geometryType )
Expand Down Expand Up @@ -692,11 +689,6 @@ void QgsVectorFileWriter::init( QString vectorFileName,
QgsDebugMsg( "Done creating fields" );

mWkbType = geometryType;
if ( mWkbType != QgsWKBTypes::NoGeometry )
{
// create geometry which will be used for import
mGeom = createEmptyGeometry( mWkbType );
}

if ( newFilename )
*newFilename = vectorFileName;
Expand Down Expand Up @@ -2133,7 +2125,9 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
}
else // wkb type matches
{
OGRErr err = OGR_G_ImportFromWkb( mGeom, const_cast<unsigned char *>( geom->asWkb() ), static_cast< int >( geom->wkbSize() ) );
OGRGeometryH ogrGeom = createEmptyGeometry( mWkbType );
OGRErr err = OGR_G_ImportFromWkb( ogrGeom, const_cast<unsigned char *>( geom->asWkb() ), static_cast< int >( geom->wkbSize() ) );

if ( err != OGRERR_NONE )
{
mErrorMessage = QObject::tr( "Feature geometry not imported (OGR error: %1)" )
Expand All @@ -2144,8 +2138,8 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
return nullptr;
}

// set geometry (ownership is not passed to OGR)
OGR_F_SetGeometry( poFeature, mGeom );
// set geometry (ownership is passed to OGR)
OGR_F_SetGeometryDirectly( poFeature, ogrGeom );
}
}
else
Expand Down Expand Up @@ -2182,11 +2176,6 @@ bool QgsVectorFileWriter::writeFeature( OGRLayerH layer, OGRFeatureH feature )

QgsVectorFileWriter::~QgsVectorFileWriter()
{
if ( mGeom )
{
OGR_G_DestroyGeometry( mGeom );
}

if ( mDS )
{
OGR_DS_Destroy( mDS );
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsvectorfilewriter.h
Expand Up @@ -517,7 +517,6 @@ class CORE_EXPORT QgsVectorFileWriter
OGRDataSourceH mDS;
OGRLayerH mLayer;
OGRSpatialReferenceH mOgrRef;
OGRGeometryH mGeom;

QgsFields mFields;

Expand Down

0 comments on commit 119e181

Please sign in to comment.