[gjs] object.c: correct memory management for singletons



commit 008394427b4dd12b5634bf551f628beb4dd85815
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 |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
---
diff --git a/gi/object.c b/gi/object.c
index 572ecd7..adb11f8 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -683,6 +683,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
     GParameter *params;
     int n_params;
     GTypeQuery query;
+    JSObject *old_jsobj;
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(object_instance);
 
@@ -705,7 +706,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.
@@ -715,7 +717,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]