gobject: forbid finalization-during-construction

If a constructor() implementation created an object but then unreffed
it rather than returning it, that object would get left on the
construction_objects list, which would cause problems later when that
memory location got reused by another object.

"Fix" this by making it fail intentionally, and add a test for it (and
for the normal, working singleton case).

https://bugzilla.gnome.org/show_bug.cgi?id=661576
This commit is contained in:
Dan Winship
2013-04-29 13:04:11 -04:00
parent a7bd6c47db
commit 0d62eb467f
4 changed files with 169 additions and 10 deletions

View File

@@ -950,6 +950,16 @@ g_object_interface_list_properties (gpointer g_iface,
return pspecs;
}
static inline gboolean
object_in_construction_list (GObject *object)
{
gboolean in_construction;
G_LOCK (construction_mutex);
in_construction = g_slist_find (construction_objects, object) != NULL;
G_UNLOCK (construction_mutex);
return in_construction;
}
static void
g_object_init (GObject *object,
GObjectClass *class)
@@ -1021,6 +1031,12 @@ g_object_real_dispose (GObject *object)
static void
g_object_finalize (GObject *object)
{
if (object_in_construction_list (object))
{
g_error ("object %s %p finalized while still in-construction",
G_OBJECT_TYPE_NAME (object), object);
}
g_datalist_clear (&object->qdata);
#ifdef G_ENABLE_DEBUG
@@ -1584,16 +1600,6 @@ slist_maybe_remove (GSList **slist,
return FALSE;
}
static inline gboolean
object_in_construction_list (GObject *object)
{
gboolean in_construction;
G_LOCK (construction_mutex);
in_construction = g_slist_find (construction_objects, object) != NULL;
G_UNLOCK (construction_mutex);
return in_construction;
}
static gpointer
g_object_new_with_custom_constructor (GObjectClass *class,
GObjectConstructParam *params,