[pygobject] Fix wrong refcount when calling introspected widget constructors



commit 7bc4122897d9d05172a2bd5b56bded87e2afaec4
Author: Steve Frécinaux <code istique net>
Date:   Sat Jan 29 00:16:50 2011 +0100

    Fix wrong refcount when calling introspected widget constructors
    
    Introspected widget constructors, like Gtk.Button.new(), can return
    objects with a floating reference, which was then reffed by pygobject,
    resulting in two references, despite the object is not owned by anyone.
    
    This patch uses ref_sink() when pygobject takes its own reference, to
    avoid adding that extra reference. Hence we now claim ownership on
    objects returned by constructors with transfer=none (which is the case
    for nearly all the widget constructors, despite the floating ref).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=640868

 gobject/pygobject.c      |    4 +++-
 tests/test_everything.py |    9 ++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index 98897fc..9be644a 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -967,7 +967,9 @@ pygobject_new_full(GObject *obj, gboolean sink, gpointer g_class)
 	self->weakreflist = NULL;
 	self->private_flags.flags = 0;
 	self->obj = obj;
-	g_object_ref(obj);
+        /* if we are creating a wrapper around a newly created object, it can have
+           a floating ref (e.g. for methods like Gtk.Button.new()). Bug 640868 */
+	g_object_ref_sink(obj);
 	pygobject_register_wrapper((PyObject *)self);
 	PyObject_GC_Track((PyObject *)self);
     }
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 701c4ad..74d917a 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -67,7 +67,14 @@ class TestEverything(unittest.TestCase):
         
 
     def test_floating(self):
-        Everything.TestFloating()
+        e = Everything.TestFloating()
+        self.assertEquals(e.__grefcount__, 1)
+
+        e = GObject.new(Everything.TestFloating)
+        self.assertEquals(e.__grefcount__, 1)
+
+        e = Everything.TestFloating.new()
+        self.assertEquals(e.__grefcount__, 1)
 
     def test_caller_allocates(self):
         struct_a = Everything.TestStructA()



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