After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 567076 - return value behaviour of gobject constructor is questionable/not defined
return value behaviour of gobject constructor is questionable/not defined
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: gobject
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2009-01-08 21:12 UTC by Stefan Sauer (gstreamer, gtkdoc dev)
Modified: 2013-05-09 15:38 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Stefan Sauer (gstreamer, gtkdoc dev) 2009-01-08 21:12:40 UTC
Lets look at this gobject constructor:
static GObject* my_xxx_constructor(GType type, guint n_construct_params, GObjectConstructParam *construct_params) {
  MyXxx *self=MY_XX(G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_params, construct_params));
  
  if(!self) return(NULL);

  if(!my_do_construct_init(self)) {
    // failed
    goto Error;
  }
  return(G_OBJECT(self));
Error:
  g_object_unref(self);
  return(NULL);
}

Questions 1:
Am I allowed to return NULL? I looked at g_object_newv() and it would do plenty calls with object=NULL. Smells bad. Is is a bug or if not allowed, shouldn't it be documented. Also if its not allowed, how do I handle failure in the constructor?

Question 2:
If its allowed, is the g_object_unref(self) okay at that point (for a not fully created object)?
Comment 1 Colin Walters 2009-01-08 21:19:38 UTC
You are not allowed to return NULL from g_object_new, as far as I know.
Comment 2 Colin Walters 2009-01-08 21:56:18 UTC
For things that can fail, a good pattern is to split it off into a separate foo_object_start method or the like, which can then also take a GError.

It's unfortunate that gobject constructors can't throw, but it's just the way things are as I understand it.
Comment 3 Stefan Sauer (gstreamer, gtkdoc dev) 2009-01-08 22:02:53 UTC
Seems you are right. A _start method sounds not that good as it would be class specific.

What basically happens is that I deserialize objects from files and use g_try_new when allocating memory based on the files. I'll think of some convention for my objects.

But it would still be good to make this more explicit in the docs. I saw several comments in e.g. gio that shows that people are not sure about it.
Comment 4 Stefan Sauer (gstreamer, gtkdoc dev) 2009-02-18 14:14:30 UTC
Its clearly not fixable in the current stable API.
Comment 5 Simon McVittie 2013-05-09 13:12:12 UTC
I think the intention these days is that g_object_new() cannot fail, but objects with failable constructors should implement GInitable.

Is there anything else to do here?
Comment 6 Stefan Sauer (gstreamer, gtkdoc dev) 2013-05-09 15:38:46 UTC
GInitable sound good. Closing this ticket.