Opened 7 years ago

Closed 7 years ago

Last modified 7 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

Description

  • 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:

#!/usr/bin/python

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

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)
field_def.SetWidth(100)
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.SetGeometryDirectly(g)

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

    l.CreateFeature(f)
    del f
del l, ds

Our gdal is built from https://github.com/koordinates/gdal/tree/trunk-kx

Change History (6)

comment:1 Changed 7 years ago by Robert Coup

Cc: Robert Coup added

comment:2 Changed 7 years ago by Robert Coup

The following code reproduces the memory leak:

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

comment:3 Changed 7 years ago by cdestigter

uh, yeah. Trac needs an edit link :)

comment:4 Changed 7 years ago by cdestigter

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

comment:5 Changed 7 years ago by Even Rouault

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 Changed 7 years ago by cdestigter

Thanks!

Note: See TracTickets for help on using tickets.