Re: Chaining of dispose and finalize



On 09/04/04 00:39, Russell Shaw wrote:
Tristan Van Berkom wrote:
Russell Shaw wrote:
[...]

But how do i do that when g_object_real_dispose() and g_object_finalize()
are both declared static in gobject.c ?


the gobject code always keeps a version of the class intact
(for quick reference at instantiation and for chaining):

parent_class =
   GTKTT_ANIM_CLASS (g_type_class_peek_parent
                    (musical_class));

Usualy people keep a global like this assigned at class_init time
in order to avoid multiple calls.

Hi,
I understand that, and is what i do. What i mean is that most
objects do in class_init something like:

  gobject_class->dispose=myobject_dispose;

But this makes the original dispose code in gobject
inaccessible. Same for gobject_class->dispose=myobject_finalize.

Obviously something very basic i'm missing here;)


Maybe i should record the parent functions in static pointers
before overiding them:


static gpointer parentdispose;
static gpointer parentfinalize;

myobject_class_init(MyObjectClass *klass){
...
  parentdispose=G_OBJECT_CLASS(parent_class)->dispose;
  parentfinalize=G_OBJECT_CLASS(parent_class)->finalize;

  G_OBJECT_CLASS(parent_class)->dispose=myobject_dispose;
  G_OBJECT_CLASS(parent_class)->finalize=myobject_finalize;
...
}

this is incorrect.  you want something like this:

...
    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
    gobject_class->dispose = myobject_dispose;
    gobject_class->finalize = myobject_finalize;

    parent_class = g_type_class_peek_parent(klass);
...

what you are trying to do is _overwrite_ the parent class' dispose and
finalized methods.  what you want to do is _override_ them, by setting
the dispose and finalize methods in _your_ class as in my example above.

then, in myobject_dispose(), you do your dispose-related stuff, and then you
chain to the parent's dispose by doing:

G_OBJECT_CLASS(parent_class)->dispose(object);

at the bottom of your dipose function.  then you need to do something similar in
myobject_finalize().  it seems the point you're not getting is that, in
your class_init() function, gobject_class is just a casted version of klass.
it is _not_ the same as the parent_class pointer that you set up with
g_type_class_peek_parent().  setting the ->dipose and ->finalize pointers
in your gobject_class does not affect those same pointers in parent_class.

if you're still not getting it, i'd suggest you look at the gtk source code.
pick a widget, and look for the _class_init() function, as well as any
_dispose() or _finalize() functions, and you'll see how it's done.

        -brian



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