summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2021-09-21 11:09:16 +0000
committerPhilip Withnall <philip@tecnocode.co.uk>2021-09-21 11:09:16 +0000
commit579ff9f6c9aa0b84df2d14634523601297807d7d (patch)
tree018bf9a1716d9c11af8fc7da5058503ebde68926
parentbbd1350beb2c261dba4b93b3740f46e287bf98f7 (diff)
parent6fe0f983607e868ed3847e781727c9912429f73e (diff)
downloadglib-579ff9f6c9aa0b84df2d14634523601297807d7d.tar.gz
Merge branch 'ebassi/floating-warning' into 'main'
Add a (diagnostic) warning for finalized floating objects Closes #2489 See merge request GNOME/glib!2247
-rw-r--r--gobject/gobject.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/gobject/gobject.c b/gobject/gobject.c
index 9ddab43f0..c05de431e 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -1182,6 +1182,24 @@ g_object_real_dispose (GObject *object)
g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL);
}
+static gboolean
+floating_check (GObject *object)
+{
+ static const char *g_enable_diagnostic = NULL;
+
+ if (G_UNLIKELY (g_enable_diagnostic == NULL))
+ {
+ g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
+ if (g_enable_diagnostic == NULL)
+ g_enable_diagnostic = "0";
+ }
+
+ if (g_enable_diagnostic[0] == '1')
+ return g_object_is_floating (object);
+
+ return FALSE;
+}
+
static void
g_object_finalize (GObject *object)
{
@@ -1191,6 +1209,17 @@ g_object_finalize (GObject *object)
G_OBJECT_TYPE_NAME (object), object);
}
+#ifdef G_ENABLE_DEBUG
+ if (floating_check (object))
+ {
+ g_critical ("A floating object %s %p was finalized. This means that someone\n"
+ "called g_object_unref() on an object that had only a floating\n"
+ "reference; the initial floating reference is not owned by anyone\n"
+ "and must be removed with g_object_ref_sink().",
+ G_OBJECT_TYPE_NAME (object), object);
+ }
+#endif
+
g_datalist_clear (&object->qdata);
GOBJECT_IF_DEBUG (OBJECTS,