Re: Chaining Up (GLib Tutorial Error)



Phil Lello wrote:
According to the GObject tutorial, chaining-up in constructor/dispose/finalize code should be handled as follows:

static void
b_method_to_call (B *obj, int a)
{
  BClass *klass;
  AClass *parent_class;
  klass = B_GET_CLASS (obj);
  parent_class = g_type_class_peek_parent (klass);

  /* do stuff before chain up */
  parent_class->method_to_call (obj, a);
  /* do stuff after chain up */
}

Yes, a tutorial mistake.
(Unless there is something horribly wrong with my implementation of _GET_CLASS...)

This works fine until we create CClass, a direct descendant of BClass. When CClass is using the default 'method_to_call' code, e.g. in finalize, B_GET_CLASS actually returns CClass. parent_class is now BClass, and 'method_to_call' invokes itself, leading eventually to stack overflow.

Yes.

The solution I've used, and that I think should be in the tutorial is:

klass = g_type_class_peek (MY_CLASS_TYPE);

Should I submit sample code to reproduce / raise a bug, or is this email sufficient? I can provide a patch to tut_howto.xml if wanted, but this seems excessive for a 1 line change.

It'd be better to use G_DEFINE_TYPE in the tutorial, IMO. A nice
macro, saves you from many troubles.

Best regards,
Yevgen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]