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

[pygame] [patch] minor memory leaks in color.c, font.c, mask.c and pixelarray.c



Hi,

I found a series of minor memory leak. minor because they only occurs in situations
where a call like PyDict_SetItemString of PyModule_AddObject fails which means
things are already pretty bad anyway.
Nevertheless here is the bug description:
In "color.c" in the "MODINIT_DEFINE (color)" function at the end the "apiobj"
leaks if the call to "PyDict_SetItemString" fails.
In "font.c" and "mask.c" the "apiobj" leaks if the call to "PyModule_AddObject"
fails. Note that normally "PyModule_AddObject" steals a reference, but it only
does so on success.
In "pixelarray.c" the leaking object is "PyPixelArray_Type" also with the failing
call to "PyModule_AddObject".

I attached a patch.

yours

//Lorenz

Index: src/font.c
===================================================================
--- src/font.c	(revision 2356)
+++ src/font.c	(working copy)
@@ -806,6 +806,7 @@
         MODINIT_ERROR;
     }
     if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj) == -1) {
+        Py_DECREF (apiobj);
         DECREF_MOD (module);
         MODINIT_ERROR;
     }
Index: src/color.c
===================================================================
--- src/color.c	(revision 2356)
+++ src/color.c	(working copy)
@@ -1608,7 +1608,6 @@
 {
     PyObject *colordict;
     PyObject *module;
-    PyObject *dict;
     PyObject *apiobj;
     static void* c_api[PYGAMEAPI_COLOR_NUMSLOTS];
     
@@ -1677,7 +1676,6 @@
         DECREF_MOD(module);
         MODINIT_ERROR;
     }
-    dict = PyModule_GetDict (module);
 
     c_api[0] = &PyColor_Type;
     c_api[1] = PyColor_New;
@@ -1689,11 +1687,11 @@
         DECREF_MOD(module);
         MODINIT_ERROR;
     }
-    if (PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
+    if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
+        Py_DECREF (apiobj);
         Py_DECREF (_COLORDICT);
         DECREF_MOD(module);
         MODINIT_ERROR;
     }
-    Py_DECREF (apiobj);
     MODINIT_RETURN (module);
 }
Index: src/pixelarray.c
===================================================================
--- src/pixelarray.c	(revision 2356)
+++ src/pixelarray.c	(working copy)
@@ -2164,6 +2164,7 @@
     if (PyModule_AddObject (module, "PixelArray",
                             (PyObject *) &PyPixelArray_Type) == -1)
     {
+        Py_DECREF ((PyObject *) &PyPixelArray_Type);
         DECREF_MOD (module);
         MODINIT_ERROR;
     }
Index: src/mask.c
===================================================================
--- src/mask.c	(revision 2356)
+++ src/mask.c	(working copy)
@@ -1538,6 +1538,7 @@
         MODINIT_ERROR;
     }
     if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj) == -1) {
+        Py_DECREF (apiobj);
         DECREF_MOD (module);
         MODINIT_ERROR;
     }