gobject: re-allow finalization from constructor()

Although returning NULL from constructor is strongly discouraged, some
old libraries need to keep doing it for ABI-compatibility reasons.
Given this, it's rude to forbid finalization from within
constructor(), since it would otherwise work correctly now anyway (and
the critical when returning NULL should discourage any new uses of
returning NULL from constructor()).

https://bugzilla.gnome.org/show_bug.cgi?id=661576
This commit is contained in:
Dan Winship 2013-12-02 11:59:30 -05:00
parent 074df39681
commit 5cab3fcec1
2 changed files with 13 additions and 11 deletions

View File

@ -1023,8 +1023,8 @@ g_object_finalize (GObject *object)
{
if (object_in_construction (object))
{
g_error ("object %s %p finalized while still in-construction",
G_OBJECT_TYPE_NAME (object), object);
g_critical ("object %s %p finalized while still in-construction",
G_OBJECT_TYPE_NAME (object), object);
}
g_datalist_clear (&object->qdata);

View File

@ -104,7 +104,6 @@ my_infanticide_object_constructor (GType type,
constructor (type, n_construct_properties, construct_params);
g_object_unref (object);
g_assert_not_reached ();
return NULL;
}
@ -120,18 +119,21 @@ my_infanticide_object_class_init (MyInfanticideObjectClass *klass)
static void
test_object_constructor_infanticide (void)
{
GObject *obj;
int i;
g_test_bug ("661576");
if (g_test_subprocess ())
for (i = 0; i < 1000; i++)
{
g_object_new (my_infanticide_object_get_type (), NULL);
g_assert_not_reached ();
return;
g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL,
"*finalized while still in-construction*");
g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL,
"*Custom constructor*returned NULL*");
obj = g_object_new (my_infanticide_object_get_type (), NULL);
g_assert_null (obj);
g_test_assert_expected_messages ();
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*finalized while still in-construction*");
g_test_trap_assert_stderr_unmatched ("*reached*");
}
/* --------------------------------- */