Re: GtkWidgets, Who owns what?



On Mon, 2014-11-17 at 09:01 -0600, The Devils Jester wrote:
I have some questions about various situations and whether or not GTK or my
program is responsible for freeing the objects/memory in these situations.

Situation 1:  I have a (visible) form, with a container, with a widget.  I
close the form.  Are all the widgets (and the form) cleaned up?

Normally yes, this of course depends on if you are just 'hiding' the
window (afaik there is no such thing as a 'form'), or destroying the
window.

Also depends on whether you have any explicit refs to any of the
children.

Situation 2:  I have the same as above, but the window is never shown, so
is never closed. However, gtk_main_quit() is called.  Am I responsible for
the cleanup of the window?  (And if I destroy the window manually, will it
clean up all of its children?)

Yes you are responsible for destroying the window, and yes, normally it
will clean up all children.

Situation 3:  I have a container, that has children, but that container is
never added to a form.  Am I responsible for cleaning up the container?
And, as above, if I destroy the container, will it clean up its children,
or am I responsible for that?

Yes, you are responsible for g_object_ref_sink()ing, destroying that
container and then unreffing it, this is an abnormal case, and yes
it will clean up it's children.


Situation 4:  I have a container with children (as in Situation 3), but it
was added to a form at one point (and later removed).  Am I responsible for
that (are we back to Situation 3 at that point?)

This depends if you held any explicit ref count to the container, if you
did not, it will automatically free itself and it's children when you
remove it from it's parent.

With those 4 situations, is the following the correct way to clean up any
of the objects that I am responsible for?

        g_object_ref_sink(G_OBJECT(widget));
        gtk_widget_destroy(widget);
        g_object_unref(G_OBJECT(widget));

This should work consistently yes.

Widgets are GInitiallyUnowned, so their ownership is consumed by
their parent widget when they are added to a parent.

The exception to the rule is GtkWindow and it's derived classes, which
is owned by GTK+'s toplevel window list, regardless of whether the
window was ever shown or not, or shown and then hidden.

Cheers,
    -Tristan




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