g_object_new: check for NULL from _constructor()
authorRyan Lortie <desrt@desrt.ca>
Fri, 26 Apr 2013 15:27:51 +0000 (11:27 -0400)
committerRyan Lortie <desrt@desrt.ca>
Fri, 26 Apr 2013 15:34:27 +0000 (11:34 -0400)
commit7d61da0c078fcc10fada292811401b127e330555
treec69fdcd7f6b3073eb2952a876513d264dee17e9e
parentc98c65fffc4319c5caca164df09af9c8415a46bd
g_object_new: check for NULL from _constructor()

There is some code in the wild (like in gnome-session) that does this
from its custom _constructor() implementation:

{
  GObject *obj;

  obj = ((chain up));

  if (!object_is_viable (obj))
    {
      g_object_unref (obj);
      return NULL;
    }
  else
    return obj;
}

This has never been a valid use of GObject and this code has always
caused memory to be leaked[1] by growing the construction_objects list.
The ability to legitimately return NULL from a constructor was exactly
the reason that we created GInitable, in fact.

That doesn't change the fact that the g_object_new() rewrite will crash
in this case, so instead of doing that, let's emit a critical and avoid
the crash.  This will allow people to upgrade their GLib without also
upgrading their gnome-session.  Meanwhile, people can fix their broken
code.

[1] not in the strictest sense of the word, because it's still reachable
gobject/gobject.c