[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] memory leaks with Py_BuildValue
- To: pygame-users@xxxxxxxx
- Subject: [pygame] memory leaks with Py_BuildValue
- From: "Campbell Barton" <ideasman42@xxxxxxxxx>
- Date: Thu, 4 Sep 2008 01:13:02 +1000
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Wed, 03 Sep 2008 11:13:07 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=uRV0WCdIzBQEsSefPGukuPiIu08UDhUCLbRrvGl8oQc=; b=OMmbgB6RF4k1Jzno7S10IlO8XGbtwtHqeLHn7Ye4ssjaU5Gzl3ncobrQU/WvCLVVlW yia0kAVhnC6O2AwniYn5XIRt8R42Q3SHmUegrqoTw7X/GLq4mALXHuInX7ehiMaFnwsR MchTxuZRS3yF1MkKVGdRkzZwiF9gz4k9IdXc8=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=DUA+FAPNdBMwt4aZKusRdqF+b6ZvFvRK37GAKsZUAhP0N/u9/UEZItzeYEIk+mD3TA kL5s7dnAcbFdEKIQ5rzifTXyfuGqT0WfvmHDZwTIBH3iDpwqij1tDXR2nq/6ZxQYH6m3 dFbYa9BBEyJ3mtido8+H1hOL7yd2Hhv9WdVbw=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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[] =