diff options
-rw-r--r-- | gobject/gobjectmodule.c | 3 | ||||
-rw-r--r-- | gobject/pygobject-private.h | 1 | ||||
-rw-r--r-- | gobject/pygobject.c | 17 | ||||
-rw-r--r-- | tests/test_subtype.py | 18 |
4 files changed, 34 insertions, 5 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index 322816b..0aa7dbd 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -1646,10 +1646,11 @@ pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs) g_type_class_unref(class); if (obj) - self = (PyGObject *) pygobject_new ((GObject *)obj); + self = (PyGObject *) pygobject_new_full((GObject *)obj, FALSE); else self = NULL; g_object_unref(obj); + pygobject_sink(obj); return (PyObject *) self; } diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h index cb7e892..c09903e 100644 --- a/gobject/pygobject-private.h +++ b/gobject/pygobject-private.h @@ -113,6 +113,7 @@ void pygobject_register_class (PyObject *dict, void pygobject_register_wrapper (PyObject *self); PyObject * pygobject_new (GObject *obj); PyObject * pygobject_new_full (GObject *obj, gboolean sink); +void pygobject_sink (GObject *obj); PyTypeObject *pygobject_lookup_class (GType gtype); void pygobject_watch_closure (PyObject *self, GClosure *closure); void pygobject_register_sinkfunc(GType type, diff --git a/gobject/pygobject.c b/gobject/pygobject.c index e0fbdda..204fdbf 100644 --- a/gobject/pygobject.c +++ b/gobject/pygobject.c @@ -33,8 +33,17 @@ typedef struct { } SinkFunc; static GArray *sink_funcs = NULL; -static inline void -sink_object(GObject *obj) +/** + * pygobject_sink: + * @obj: a GObject + * + * As Python handles reference counting for us, the "floating + * reference" code in GTK is not all that useful. In fact, it can + * cause leaks. This function should be called to remove the floating + * references on objects on construction. + **/ +void +pygobject_sink(GObject *obj) { if (sink_funcs) { gint i; @@ -517,7 +526,7 @@ pygobject_register_wrapper(PyObject *self) { GObject *obj = ((PyGObject *)self)->obj; - sink_object(obj); + pygobject_sink(obj); Py_INCREF(self); g_object_set_qdata_full(obj, pygobject_wrapper_key, self, pyg_destroy_notify); @@ -716,7 +725,7 @@ pygobject_new_full(GObject *obj, gboolean sink) return NULL; self->obj = g_object_ref(obj); if (sink) - sink_object(self->obj); + pygobject_sink(self->obj); self->inst_dict = NULL; self->weakreflist = NULL; diff --git a/tests/test_subtype.py b/tests/test_subtype.py index a48452c..630344c 100644 --- a/tests/test_subtype.py +++ b/tests/test_subtype.py @@ -52,3 +52,21 @@ class TestSubType(unittest.TestCase): continue subname = name + "PyGtkTestSubclass" sub = type(subname, (cls,), {'__gtype_name__': subname }) + + def testGtkWindowObjNewRefcount(self): + foo = gobject.new(gtk.Window) + self.assertEqual(foo.__grefcount__, 2) + + def testGtkWindowFactoryRefcount(self): + foo = gtk.Window() + self.assertEqual(foo.__grefcount__, 2) + + def testPyWindowObjNewRefcount(self): + PyWindow = type('PyWindow', (gtk.Window,), dict(__gtype_name__='PyWindow1')) + foo = gobject.new(PyWindow) + self.assertEqual(foo.__grefcount__, 2) + + def testGtkWindowFactoryRefcount(self): + PyWindow = type('PyWindow', (gtk.Window,), dict(__gtype_name__='PyWindow2')) + foo = PyWindow() + self.assertEqual(foo.__grefcount__, 2) |