Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix large memory leak in vector file writer (fix #16003)
Every written geometry is being leaked... ouch!
  • Loading branch information
nyalldawson committed Feb 7, 2017
1 parent 15dcb6f commit 826a867
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
19 changes: 4 additions & 15 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -79,7 +79,6 @@ QgsVectorFileWriter::QgsVectorFileWriter(
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
, mGeom( nullptr )
, mError( NoError )
, mCodec( nullptr )
, mWkbType( geometryType )
Expand Down Expand Up @@ -108,7 +107,6 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName,
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
, mGeom( nullptr )
, mError( NoError )
, mCodec( nullptr )
, mWkbType( geometryType )
Expand Down Expand Up @@ -622,11 +620,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 @@ -2069,7 +2062,8 @@ OGRFeatureH QgsVectorFileWriter::createFeature( const QgsFeature& feature )
else // wkb type matches
{
QByteArray wkb( geom.exportToWkb() );
OGRErr err = OGR_G_ImportFromWkb( mGeom, reinterpret_cast<unsigned char *>( const_cast<char *>( wkb.constData() ) ), wkb.length() );
OGRGeometryH ogrGeom = createEmptyGeometry( mWkbType );
OGRErr err = OGR_G_ImportFromWkb( ogrGeom, reinterpret_cast<unsigned char *>( const_cast<char *>( wkb.constData() ) ), wkb.length() );
if ( err != OGRERR_NONE )
{
mErrorMessage = QObject::tr( "Feature geometry not imported (OGR error: %1)" )
Expand All @@ -2080,8 +2074,8 @@ OGRFeatureH QgsVectorFileWriter::createFeature( const 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 @@ -2118,11 +2112,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 @@ -523,7 +523,6 @@ class CORE_EXPORT QgsVectorFileWriter
OGRDataSourceH mDS;
OGRLayerH mLayer;
OGRSpatialReferenceH mOgrRef;
OGRGeometryH mGeom;

QgsFields mFields;

Expand Down

0 comments on commit 826a867

Please sign in to comment.