Re: GObject reference counting / lack of "sink" issue



Tim Janik wrote:

> you are right, GObject is widely used these days out of GtkObject
> contexts,
> and anywhere in C land (where memory book keeping or reference house
> keeping
> can't be automized) when objects are created and ownership is passed on,
> a floating flag is strongly desired (and forces you to derive and
> reimplement
> if you're consequent enough ;)

>From a language bindings perspective, if the language supports garbage
collection you usually want to get rid of the floating references if
they exist.  So having support for floating references in GObject itself
would be good, provided all objects with floating references used the
support, since the "remove floating reference" code could be generic.

(it would still be necessary to special case GtkWindow and its
descendants due to the fact that the reference returned by
g_object_new(GTK_TYPE_WINDOW, ...) is owned by the GTK internals).

> so for a change, i'd like to suggest introducing extra API (and do
> some slight
> deprecations) for this and apprechiate people's comments on it:
>
> /* ref() and clear floating flag (#1) */
> GObject*    g_object_ref_sink       (GObject *object);
>
> /* figure whether floating flag is set */
> gboolean    g_object_is_floating    (GObject *object);
>
> /* intended for object implementations only, sets the floating flag */
> void        g_object_force_floating (GObject *object);

This API looks usable.  So the following code:
    if (GTK_OBJECT_FLOATING(object)) {
        g_object_ref(object);
        gtk_object_sink(GTK_OBJECT(object));
    }

to:
    if (g_object_is_floating(object))
        g_object_ref_sink(object);

> #1) in allmost all cases where the _sink() API is used, it's used in
>     combination with _ref() similar to:
>       g_object_ref (object);
>       g_object_sink (object);
>     so this may as well be a single function call, especially since
>     this function call may then conveniently return the object
>     handle the way _ref() already does it. in the pretty rare
>     scenarios where the automatically added reference count is not
>     usefull (e.g. when emulating gtk_object_sink()), a combination of
>     _ref_sink() and _unref() can be used instead. 

Combining ref() and sink() into a single call would also make it
impossible to issue the sink() call before ref(), closing off a class of
bugs (it does open up a similar class of bugs for implementing sink()
behaviour, but that should be a lot less common).

James.



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