[gjs: 1/2] object: Warn about finalized GObject in fewer cases



commit 8cdd6291325d96077170bf47f9a83899229cee52
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jun 2 15:15:14 2018 -0700

    object: Warn about finalized GObject in fewer cases
    
    We want to remove this warning in cases where the subsequent code isn't
    actually trying to access the underlying GObject and therefore isn't
    going to crash. On resolve() we don't want the warning, as that only
    applies to prototype objects. On trace() we don't want it either, as that
    may legitimately be called during the GC mark phase on a JS wrapper with
    a disposed GObject.
    
    In the typecheck function, we only use the GObject for a debug assertion.
    All the other checks use the GType. We relax the assertion and only do
    the debug sanity check if the GObject is still alive. Instead of logging
    the warning here, we log the warning in gjs_g_object_from_object() which
    happens later when the GObject is actually used.
    
    We keep the warnings in property getters and setters, since those would
    access the underlying GObject, and they are now only called for names
    that really do resolve to GObject properties and fields.
    
    Closes: #24

 gi/object.cpp | 47 ++++++++++++++---------------------------------
 1 file changed, 14 insertions(+), 33 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 066281b0..f9417df9 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -862,17 +862,6 @@ object_instance_resolve(JSContext       *context,
         return true;
     }
 
-    if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already finalized. "
-                   "Impossible to resolve it.",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
-        *resolved = false;
-        return true;
-    }
-
     GjsAutoJSChar name;
     if (!gjs_get_string_id(context, id, &name)) {
         *resolved = false;
@@ -1705,15 +1694,6 @@ object_instance_trace(JSTracer *tracer,
     if (priv == NULL)
         return;
 
-    if (priv->g_object_finalized) {
-        g_debug("Object %s.%s (%p), has been already finalized. "
-                "Impossible to trace it.",
-                 priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                 priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                 priv->gobj);
-        return;
-    }
-
     for (GClosure *closure : priv->closures)
         gjs_closure_trace(closure, tracer);
 
@@ -2418,6 +2398,19 @@ gjs_g_object_from_object(JSContext       *context,
         return NULL;
 
     priv = priv_from_js(context, obj);
+
+    if (priv->g_object_finalized) {
+        g_critical("Object %s.%s (%p), has been already deallocated - "
+                   "impossible to access it. This might be caused by the "
+                   "object having been destroyed from C code using something "
+                   "such as destroy(), dispose(), or remove() vfuncs",
+                   priv->info ? g_base_info_get_namespace(priv->info) : "",
+                   priv->info ? g_base_info_get_name(priv->info) : g_type_name(priv->gtype),
+                   priv->gobj);
+        gjs_dumpstack();
+        return nullptr;
+    }
+
     return priv->gobj;
 }
 
@@ -2464,19 +2457,7 @@ gjs_typecheck_object(JSContext       *context,
         return false;
     }
 
-    if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already deallocated - impossible to access to it. "
-                   "This might be caused by the fact that the object has been destroyed from C "
-                   "code using something such as destroy(), dispose(), or remove() vfuncs",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
-
-        return true;
-    }
-
-    g_assert(priv->gtype == G_OBJECT_TYPE(priv->gobj));
+    g_assert(priv->g_object_finalized || priv->gtype == G_OBJECT_TYPE(priv->gobj));
 
     if (expected_type != G_TYPE_NONE)
         result = g_type_is_a (priv->gtype, expected_type);


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