Re: Chaining Up (GLib Tutorial Error)
- From: Stefan Kost <ensonic hora-obscura de>
- To: Yevgen Muntyan <muntyan tamu edu>
- Cc: gtk-devel-list gnome org, Phil Lello <phil lello googlemail com>
- Subject: Re: Chaining Up (GLib Tutorial Error)
- Date: Sun, 28 Oct 2007 18:04:33 +0200
Yevgen Muntyan schrieb:
> 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.
Why? The major difference is that app do this:
static GObjectClass *parent_class = NULL;
xxx_class_init (GstElementClass * klass)
{
parent_class = g_type_class_peek_parent (klass);
...
}
and later e.g.
G_OBJECT_CLASS (parent_class)->finalize (object);
to chainup.
>> (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.
>>
B_GET_CLASS should always give you BClass and never CClass imho.
>
> 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.
>
You're very welcome to do it. Its planned for a long time. IMHO the current
details explanation should stay there as well. What should be added is more
something like : "no that you've read about the internals, this is how you can
do it in practise and save typing".
Stefan
>
> Best regards,
> Yevgen
>
> _______________________________________________
> gtk-devel-list mailing list
> gtk-devel-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-devel-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]