[pygobject/pygobject-2-28] Try not to sink objects returned by C functions.



commit b113540b452638601945f63297e68b7564c3d4df
Author: Steve Frécinaux <code istique net>
Date:   Mon Mar 7 01:58:36 2011 +0100

    Try not to sink objects returned by C functions.
    
    When creating a wrapper around an object to call a python function, we
    try to avoid calling pygobject_sink() when the object was not created by
    pygobject or the static bindings because otherwise we can end up leaking
    one reference for GInitiallyUnowned subclasses if the object was already
    owned by someone else.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=644067

 gobject/pygobject-private.h |    1 +
 gobject/pygobject.c         |    7 +++++++
 gobject/pygtype.c           |    6 +++---
 3 files changed, 11 insertions(+), 3 deletions(-)
---
diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h
index 99c1894..6f3a25e 100644
--- a/gobject/pygobject-private.h
+++ b/gobject/pygobject-private.h
@@ -156,6 +156,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, gpointer g_class);
+PyObject *    pygobject_new_sunk         (GObject *obj);
 void          pygobject_sink             (GObject *obj);
 PyTypeObject *pygobject_lookup_class     (GType gtype);
 void          pygobject_watch_closure    (PyObject *self, GClosure *closure);
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index 3e77f99..e6dfbc4 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -988,6 +988,13 @@ pygobject_new(GObject *obj)
     return pygobject_new_full(obj, TRUE, NULL);
 }
 
+PyObject *
+pygobject_new_sunk(GObject *obj)
+{
+    g_object_set_qdata (obj, pygobject_ref_sunk_key, GINT_TO_POINTER (1));
+    return pygobject_new_full(obj, TRUE, NULL);
+}
+
 static void
 pygobject_unwatch_closure(gpointer data, GClosure *closure)
 {
diff --git a/gobject/pygtype.c b/gobject/pygtype.c
index 40d9b42..a8c19d8 100644
--- a/gobject/pygtype.c
+++ b/gobject/pygtype.c
@@ -1012,7 +1012,7 @@ pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
     switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) {
     case G_TYPE_INTERFACE:
 	if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT))
-	    return pygobject_new(g_value_get_object(value));
+	    return pygobject_new_sunk(g_value_get_object(value));
 	else
 	    break;
     case G_TYPE_CHAR: {
@@ -1130,7 +1130,7 @@ pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
     case G_TYPE_PARAM:
 	return pyg_param_spec_new(g_value_get_param(value));
     case G_TYPE_OBJECT:
-	return pygobject_new(g_value_get_object(value));
+	return pygobject_new_sunk(g_value_get_object(value));
     default:
 	{
 	    PyGTypeMarshal *bm;
@@ -1326,7 +1326,7 @@ pyg_signal_class_closure_marshal(GClosure *closure,
     g_return_if_fail(object != NULL && G_IS_OBJECT(object));
 
     /* get the wrapper for this object */
-    object_wrapper = pygobject_new(object);
+    object_wrapper = pygobject_new_sunk(object);
     g_return_if_fail(object_wrapper != NULL);
 
     /* construct method name for this class closure */



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