Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#5623 closed defect (fixed)

CSV driver: memory leak when geometries are supplied but not used

Reported by: cdestigter Owned by: warmerdam
Priority: normal Milestone: 1.11.1
Component: OGR_SF Version: 1.11.0
Severity: normal Keywords: csv SetGeometryDirectly failure
Cc: Robert Coup


  • create a CSV datasource
  • create a bunch of features with geometries set using SetGeometryDirectly
  • use the default creation options (ie discard geometries in CSV output)

The process will leak memory like a sieve.

Using SetGeometry, or using GEOMETRY=AS_WKT or GEOMETRY=AS_XY avoids the problem.

The following code reproduces the memory leak:


# Try with `valgrind --leak-check=full /usr/bin/python ./`

from osgeo import ogr

ogrdriver = ogr.GetDriverByName("CSV")
ds = ogrdriver.CreateDataSource("memleak.csv")
l = ds.CreateLayer("memleak", options=['GEOMETRY=AS_WKT'])
l_defn = l.GetLayerDefn()

field_def = ogr.FieldDefn("field1", ogr.OFTString)
assert not l.CreateField(field_def)

for i in xrange(5000):
    if i % 500 == 0:
        print i
    f = ogr.Feature(l_defn)

    g = ogr.CreateGeometryFromWkt('POINT(%.3f %.3f)' % (i, i))

    f.SetField("field1", str(i))

    del f
del l, ds

Our gdal is built from

Change History (6)

comment:1 by Robert Coup, 10 years ago

Cc: Robert Coup added

comment:2 by Robert Coup, 10 years ago

The following code reproduces the memory leak:

if you comment out options=['GEOMETRY=AS_WKT'] obviously...

comment:3 by cdestigter, 10 years ago

uh, yeah. Trac needs an edit link :)

comment:4 by cdestigter, 10 years ago

I should also mention I tried ESRI Shapefile and GPKG drivers, and neither exhibited this memory leak.

comment:5 by Even Rouault, 10 years ago

Keywords: SetGeometryDirectly failure added
Milestone: 1.11.1
Resolution: fixed
Status: newclosed
Version: svn-trunk1.11.0

trunk r27592, branches/1.11 r27593: "Fix OGRFeature::SetGeometryDirectly() and SetGeomFieldDirectly() to free the passed geometry even if the method fails (#5623)"

comment:6 by cdestigter, 10 years ago


Note: See TracTickets for help on using tickets.