[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] memory leaks with Py_BuildValue



Hi, over a year ago I had a look at pygames py-c/api usage for memory
leaks, and found a few, mainly with Py_BuildValue() using PyObjects
which are incref'd,
had another look at the recent SVN and found some more, not sure if
they are new but they definitely cause memory leaks.

For example - call pygame.mouse.get_cursor() in a loop of 10000 for
every update in chimp.py and memory will go up 100's of meg fairly
fast, patch below.

you also might want to consider using PyTuple_Pack() which is faster
but only in python 2.4 or later.
http://docs.python.org/api/tupleObjects.html#l2h-583


These leak too, much the same changes need to be made here also.
./src/mask.c:265:        return Py_BuildValue ("(OO)", PyInt_FromLong(m10/m00),
./src/mask.c:268:        return Py_BuildValue ("(OO)",
PyInt_FromLong(0), PyInt_FromLong(0));
./src/mask.c:298:        return Py_BuildValue ("O", PyFloat_FromDouble(theta));
./src/mask.c:300:        return Py_BuildValue ("O", PyFloat_FromDouble(0));


Index: src/mouse.c
===================================================================
--- src/mouse.c	(revision 1645)
+++ src/mouse.c	(working copy)
@@ -171,7 +171,7 @@
 mouse_get_cursor (PyObject* self)
 {
     SDL_Cursor *cursor = NULL;
-    PyObject* xordata, *anddata;
+    PyObject* xordata, *anddata, *ret;
     int size, loop, w, h, spotx, spoty;

     VIDEO_INIT_CHECK ();
@@ -201,8 +201,10 @@
         PyTuple_SET_ITEM (xordata, loop, PyInt_FromLong (cursor->data[loop]));
         PyTuple_SET_ITEM (anddata, loop, PyInt_FromLong (cursor->mask[loop]));
     }
-
-    return Py_BuildValue ("((ii)(ii)OO)", w, h, spotx, spoty,
xordata, anddata);
+	ret = Py_BuildValue ("((ii)(ii)OO)", w, h, spotx, spoty, xordata, anddata);
+	Py_DECREF (xordata);
+	Py_DECREF (anddata);
+    return ret;
 }

 static PyMethodDef mouse_builtins[] =