Re: Weak references for GObject



I discussed this issue for a long while wth Tim this morning; 
our concluding scheme was somewhat different than my original
proposal. 


Tim's main concern was that people would want to create object
types like GtkObject, with explicit destruction; every such
object type would have:

 * Its own gtk_object_destroy() public function for destruction
 * Its own virtual function equivalent to ->destroy() in GtkObject

This causes code duplication of tricky code portions and causes
a lack of unity across GObject using code.


So, what Tim proposed instead was to basically move the 
destroy() virtual function into GObject and call it dispose().

In detail:

 * A dispose() virtual method would be added to the GObjectClass
   structure.

 * This dispose() virtual method is:

   - Called when someone explicitely calls g_object_dispose()
   - Called when the reference count

 * Signal emission from the dispose() virtual method works. 
   Resurrection will probably work too, though I don't know
   if we want to guarantee this long-term.

 * The dispose() implementation for GtkObject does:

   gtk_signal_emit (object, "destroy");
   parent_class->dispose (object);

 * The dispose() implementation for GObject removes all signals
   and notifies all weak references.

 * We remove the shutdown() virtual method in GObject since
   it is no longer needed. [1]

So, for completely new GTK+-based code you would:

 * Implement what you currently do in ->destroy() in ->dispose()
 * Call g_object_dispose() instead of gtk_widget_destroy()
 * Use g_object_weak_ref() instead of connections to ::destroy.
   (There is no ::dispose signal) [2]


While I think this scheme does introduce some questionable
practices from into GObject into GTK+, one big advantage of it
is that the relationship between the GtkObject life-cycle
and the GObject life-cycle is considerably simplified.

Regards,
                                        Owen


[1] This does mean that there is no way to do caching on last-ref,
    as you can do currently with ->shutdown(). You can't use
    dispose() for this, since there is no protection against
    random strangers calling g_object_dispose() on your object.
 
    People, of course, _shouldn't_ call g_object_dispose() on an object
    unless it conceptually works like GtkObject, but we have 
    no protection against people doing so in this scheme.

[2] There are several reasons for this: 
     a) dispose() isn't going to act like a normal signal anyways; 
        for example, "after" connections don't work 
     b) emitting both ::dispose
        and ::destroy for GtkObject would be expensive 
     c) Using a signal for this may hinder future migration
        to better memory management schemes.




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