]> granicus.if.org Git - python/commitdiff
ctypes CThunkObject was not registered correctly with the cycle
authorThomas Heller <theller@ctypes.org>
Tue, 23 Feb 2010 20:11:44 +0000 (20:11 +0000)
committerThomas Heller <theller@ctypes.org>
Tue, 23 Feb 2010 20:11:44 +0000 (20:11 +0000)
garbage collector, leading to possible leaks when using callback
functions.

Lib/ctypes/test/test_callbacks.py
Misc/NEWS
Modules/_ctypes/callbacks.c

index b9e662804bc1d9d3173812986693e0257df27a35..6f517b1c113d3dcd38f4ca9abc756511aece9144 100644 (file)
@@ -118,6 +118,22 @@ class Callbacks(unittest.TestCase):
         prototype = self.functype.im_func(object)
         self.assertRaises(TypeError, prototype, lambda: None)
 
+    def test_issue_7959(self):
+        proto = self.functype.im_func(None)
+
+        class X(object):
+            def func(self): pass
+            def __init__(self):
+                self.v = proto(self.func)
+
+        import gc
+        for i in range(32):
+            X()
+        gc.collect()
+        live = [x for x in gc.get_objects()
+                if isinstance(x, X)]
+        self.failUnlessEqual(len(live), 0)
+
 try:
     WINFUNCTYPE
 except NameError:
index 1345a56b6e40a50db30b31edbc31d7ec3bb08c4c..ff04b289d9c788115878a1bff51c423adc19ea9e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7959: ctypes callback functions are now registered correctly
+  with the cylce garbage collector.
+
 - Issue #7970: email.Generator.flatten now correctly flattens message/rfc822
   messages parsed by email.Parser.HeaderParser.
 
index a643970586a80f345c6d23cb9ba8134a38b5dc23..afdd9472f45e819f12f87338b9b2a2579e613037 100644 (file)
@@ -23,7 +23,7 @@ CThunkObject_dealloc(PyObject *_self)
        Py_XDECREF(self->restype);
        if (self->pcl)
                _ctypes_free_closure(self->pcl);
-       PyObject_Del(self);
+       PyObject_GC_Del(self);
 }
 
 static int
@@ -66,7 +66,7 @@ PyTypeObject PyCThunk_Type = {
        0,                                      /* tp_getattro */
        0,                                      /* tp_setattro */
        0,                                      /* tp_as_buffer */
-       Py_TPFLAGS_DEFAULT,                     /* tp_flags */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,                        /* tp_flags */
        "CThunkObject",                         /* tp_doc */
        CThunkObject_traverse,                  /* tp_traverse */
        CThunkObject_clear,                     /* tp_clear */
@@ -385,7 +385,7 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
        CThunkObject *p;
        int i;
 
-       p = PyObject_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
+       p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
        if (p == NULL) {
                PyErr_NoMemory();
                return NULL;
@@ -400,6 +400,7 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
        
        for (i = 0; i < nArgs + 1; ++i)
                p->atypes[i] = NULL;
+       PyObject_GC_Track((PyObject *)p);
        return p;
 }