[gjs/gjs-1-30: 2/3] object.c: correct memory management for singletons



commit c8f2f22671b146b633cd5a507c9140e516f3e7c1
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Thu Jan 12 19:51:18 2012 -0500

    object.c: correct memory management for singletons
    
    Make sure we don't try to remove a toggle reference that we
    don't add, and unreference the extra reference created by
    g_object_new().

 gi/object.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
---
diff --git a/gi/object.c b/gi/object.c
index 05988fe..b8edb3c 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -675,6 +675,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
             GParameter *params;
             int n_params;
             GTypeQuery query;
+            JSObject *old_jsobj;
 
             gtype = g_registered_type_info_get_g_type( (GIRegisteredTypeInfo*) priv->info);
             if (gtype == G_TYPE_NONE) {
@@ -693,7 +694,8 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
             priv->gobj = g_object_newv(gtype, n_params, params);
             free_g_params(params, n_params);
 
-            if (peek_js_obj(context, priv->gobj) != NULL) {
+            old_jsobj = peek_js_obj(context, priv->gobj);
+            if (old_jsobj != NULL) {
                 /* g_object_newv returned an object that's already tracked by a JS
                  * object. Let's assume this is a singleton like IBus.IBus and return
                  * the existing JS wrapper object.
@@ -703,8 +705,9 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
                  * we're not actually using it, so just let it get collected. Avoiding
                  * this would require a non-trivial amount of work.
                  * */
-                object = peek_js_obj(context, priv->gobj);
-
+                object = old_jsobj;
+                g_object_unref(priv->gobj); /* We already own a reference */
+                priv->gobj = NULL;
                 goto out;
             }
 



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