[pygi] Correct the reference counting of userdata in closure handling



commit 8a0c48f4dd512797e5cf132f8ec6fb6d4d1e7aaa
Author: Zach Goldberg <zach zachgoldberg com>
Date:   Sun Apr 25 15:09:08 2010 -0400

    Correct the reference counting of userdata in closure handling
    
    Without this we lose references on every call and eventually end up free'ing objects
    while they are still in use.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=616786

 gi/pygi-closure.c        |    3 +++
 tests/test_everything.py |   13 +++++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
---
diff --git a/gi/pygi-closure.c b/gi/pygi-closure.c
index caef5ea..f452068 100644
--- a/gi/pygi-closure.c
+++ b/gi/pygi-closure.c
@@ -82,6 +82,9 @@ _pygi_closure_handle (ffi_cif *cif,
                             PyErr_Print();
                             goto end;
                         }
+                        
+                        Py_XINCREF(closure->user_data);
+
                         n_in_args++;
                         continue;
                     }
diff --git a/tests/test_everything.py b/tests/test_everything.py
index ce5ca95..00b7f95 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -143,3 +143,16 @@ class TestCallbacks(unittest.TestCase):
 
         Everything.test_multi_callback(callback)
         self.assertEquals(TestCallbacks.called, 2)
+
+    def testCallbackUserdata(self):
+        TestCallbacks.called = 0
+        def callback(userdata):
+            self.assertEquals(userdata, "Test%d" % TestCallbacks.called)
+            TestCallbacks.called += 1
+            return TestCallbacks.called
+        
+        for i in range(100):
+            val = Everything.test_callback_user_data(callback, "Test%d" % i)
+            self.assertEquals(val, i+1)
+            
+        self.assertEquals(TestCallbacks.called, 100)



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