gobject: re-allow finalization from constructor()
authorDan Winship <danw@gnome.org>
Mon, 2 Dec 2013 16:59:30 +0000 (11:59 -0500)
committerDan Winship <danw@gnome.org>
Sat, 15 Feb 2014 15:20:53 +0000 (10:20 -0500)
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

gobject/gobject.c
gobject/tests/object.c

index 29bcccd..e12eb6b 100644 (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);
index f6e75d5..3e85995 100644 (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*");
 }
 
 /* --------------------------------- */