Next: , Previous: GtkMisc, Up: Top


132 GtkObject

The base class of the GTK+ type hierarchy

132.1 Overview

132.2 Description

<gtk-object> is the base class for all widgets, and for a few non-widget objects such as <gtk-adjustment>. <gtk-object> predates <gobject>; non-widgets that derive from <gtk-object> rather than <gobject> do so for backward compatibility reasons.

<gtk-object>s are created with a "floating" reference count. This means that the initial reference is not owned by anyone. Calling g-object-unref on a newly-created <gtk-object> is incorrect, the floating reference has to be removed first. This can be done by anyone at any time, by calling g-object-ref-sink to convert the floating reference into a regular reference. g-object-ref-sink returns a new reference if an object is already sunk (has no floating reference).

When you add a widget to its parent container, the parent container will do this: This means that the container now owns a reference to the child widget and the child widget has no floating reference.

     
       g_object_ref_sink (G_OBJECT (child_widget));

The purpose of the floating reference is to keep the child widget alive until you add it to a parent container:

     
        button = gtk_button_new ();
        /* button has one floating reference to keep it alive */
        gtk_container_add (GTK_CONTAINER (container), button);
        /* button has one non-floating reference owned by the container */

<gtk-window> is a special case, because GTK+ itself will ref/sink it on creation. That is, after calling gtk-window-new, the <gtk-window> will have one reference which is owned by GTK+, and no floating references.

One more factor comes into play: the "destroy" signal, emitted by the gtk-object-destroy method. The "destroy" signal asks all code owning a reference to an object to release said reference. So, for example, if you call gtk-object-destroy on a <gtk-window>, GTK+ will release the reference count that it owns; if you call gtk-object-destroy on a <gtk-button>, then the button will be removed from its parent container and the parent container will release its reference to the button. Because these references are released, calling gtk-object-destroy should result in freeing all memory associated with an object, unless some buggy code fails to release its references in response to the "destroy" signal. Freeing memory (referred to as finalization only happens if the reference count reaches zero.

Some simple rules for handling <gtk-object:>

Never call g-object-unref unless you have previously called g-object-ref, even if you created the <gtk-object>. (Note: this is not true for <gobject>; for <gobject>, the creator of the object owns a reference.)

Call gtk-object-destroy to get rid of most objects in most cases. In particular, widgets are almost always destroyed in this way.

Because of the floating reference count, you don't need to worry about reference counting for widgets and toplevel windows, unless you explicitly call g-object-ref yourself.

132.3 Usage

— Class: <gtk-object>

Derives from <g-initially-unowned>.

This class defines the following slots:

user-data
Anonymous User Data Pointer
— Signal on <gtk-object>: destroy

Signals that all holders of a reference to the <gtk-object> should release the reference that they hold. May result in finalization of the object if all references are released.