Opened 16 years ago

Closed 14 years ago

#2002 closed defect (fixed)

python bindings are leaking memory

Reported by: Even Rouault Owned by: hobu
Priority: normal Milestone: 1.7.0
Component: PythonBindings Version: svn-trunk
Severity: normal Keywords:
Cc: warmerdam, Kyle Shannon

Description

I have run autotest under valgrind and found memory leaks in GDAL itself (most of them fixed now), but also in the python bindings themselves. I think one of them is OGRGeometry not being freed. Even without Valgrind, you can see it with the modified ogr_wkbwkt_geom.py (geometries created and destroyed in a huge loop). The python process memory doesn't stop increasing.

Attachments (3)

ogr_wkbwkt_geom_bigloop.py (5.3 KB ) - added by Even Rouault 16 years ago.
Modified version of ogr_wkbwkt_geom.py
ogr_wkbwkt_geom_valgrind_log.txt (18.4 KB ) - added by Even Rouault 16 years ago.
Extract from "valgrind --trace-children=yes --leak-check=full ./ogr_wkbwkt_geom.py"
remaining_leaks_after_r12855.txt (16.5 KB ) - added by Even Rouault 16 years ago.

Download all attachments as: .zip

Change History (14)

by Even Rouault, 16 years ago

Attachment: ogr_wkbwkt_geom_bigloop.py added

Modified version of ogr_wkbwkt_geom.py

by Even Rouault, 16 years ago

Extract from "valgrind --trace-children=yes --leak-check=full ./ogr_wkbwkt_geom.py"

comment:1 by hobu, 16 years ago

I think this is fixed in r12851 for the ng bindings. Please regenerate and confirm.

by Even Rouault, 16 years ago

comment:2 by hobu, 16 years ago

Cc: warmerdam added

comment:3 by hobu, 16 years ago

Unless they are obvious or simple, I don't think we can do too much more on this one for 1.5. Two of the leaks (_Dict and ReadDir) are related to swig typemaps that would need to be changed for all languages. It is too late for those kinds of changes...

comment:4 by hobu, 16 years ago

Milestone: 1.5.01.6.0

Kicking forward to 1.6...

comment:5 by Even Rouault, 15 years ago

Howard, I think the following patch fixes the leak in _Dict.

Index: include/python/typemaps_python.i
===================================================================
--- include/python/typemaps_python.i    (révision 15995)
+++ include/python/typemaps_python.i    (copie de travail)
@@ -375,6 +375,8 @@
         PyObject *nm = PyString_FromString( keyptr );
         PyObject *val = PyString_FromString( valptr );
         PyDict_SetItem($result, nm, val );
+        Py_DECREF(nm);
+        Py_DECREF(val);
         CPLFree( keyptr );
       }
       stringarray++;
@@ -484,6 +486,27 @@
   }
 }

It fixes leaks for following snippet :

import gdal
for i in range(1000000):
    md = drv.GetMetadata()
    print md['DMD_EXTENSION']

comment:6 by Even Rouault, 15 years ago

Arg, I meant :

import gdal
drv = gdal.GetDriverByName('GTiff')
for i in range(100000):
    md = drv.GetMetadata()
    print md['DMD_EXTENSION']

comment:7 by Even Rouault, 15 years ago

Milestone: 1.6.11.7.0

I've commited the above fix, as well as another one that would need work in other bindings, in r16031

comment:8 by Even Rouault, 15 years ago

Fix memory leak in Python binding of "typemap(out) char dict" typemap (#2002) backported in branches/1.6 in r16034

comment:9 by Even Rouault, 15 years ago

Memory leaks when translating python sequence to C arrays fixed in trunk (16452) and in branches/1.6 (r16453)

comment:10 by Kyle Shannon, 15 years ago

Cc: Kyle Shannon added

comment:11 by Even Rouault, 14 years ago

Resolution: fixed
Status: newclosed

I am not aware of any remaining leaks in python bindings. Closing

Note: See TracTickets for help on using tickets.