Commit 4af33fa2 authored by Tim Janik's avatar Tim Janik
Browse files

hm, initital refcount revolution commit ;)

still some gnits left, but keep working on it ;)
parent ee7038f9
......@@ -7,10 +7,14 @@ functions that follow these conventions:
*_new: Create a new structure with a reference count of 1.
*_ref: Increase ref count by one.
*_unref: Decrease ref count by one. If the count drops to zero,
free the memory. No user visible actions should take place,
like destryoing windows, etc.
run aprropriate finalization code and free the memory.
No user visible actions should take place, like
destryoing windows, etc.
Some structures also provide a *_destroy function.
Some structures also provide a *_destroy function, but it is generally
unrelated to freeing the memory. `Destroying' merely renders an
object `unusable'. But as long as there are references to it, it will
stick around.
......@@ -28,7 +32,7 @@ ref_count is updated accordingly.
You can call gdk_window_destroy more than once on a particular
GdkWindow, it will only be destroyed when it hasn't been yet. The
ref_count is *always* decremented, tho.
ref_count is *always* decremented, tho. Be careful.
......@@ -86,7 +90,112 @@ There is no gtk_style_destroy function.
This one is the most tricky and I'm still meditating over it.
GtkObjects follow the usual ref_counting strategy, but with a twist.
They are created with a ref_count of 1. GtkObjects are able able to
run finalization code when the ref_count drops to zero but you cannot
register arbitrary signal handlers to run at finalization time.
There is also the old gtk_object_destroy function and the "destroy"
signal but they are somewhat independent from finalization. Just as
stated at the top of this text, gtk_object_destroy merely renders an
object unusable. When the object is a container widget for example,
it unrealizes that widget, removes all children and disconnects all
signal handlers. The finalization code is different, it would for
example free associated memory for text strings and release the
attached style.
[This is the biggest change. Every widget must be revised to have a
proper "destroy" function, etc. Such a destroy function must be able
to be called any number of times and generally leave the widget in a
minimal but consistent state. The "finalization" function is new and
should perform last-minute cleanup actions. It can assume that the
"destroy" function has been called as the last function on this
Essentially, the old "destroy" function has been split into a
"finalize" plus a "destroy" function.]
It is not possible to create GtkObjects with a ref_count of 0 (as it
is done now) because the first ref/unref pair will destroy it
To be mostly backward compatible with existing practice, a GtkObject
leads a more complicated life than the other reference counted structures.
When a GtkObject is created, it starts out in a special state called
"floating" (this is the twist). This means that it is alive and has a
reference to it, but the `owner' of this reference is not known.
There are certain `potential owners' that will adopt a floating
GtkObject. For GtkWidgets the most common adopters are the parent
When you want to adopt a possibly floating GtkObject, you call
gtk_object_sink on it. This clears the floating state of the
GtkObject and decrements the ref_count by one, if it has been floating
previously. Once the floating state has been cleared, it will never
be set again.
All widgets that are part of the display are linked into a
parent/child tree. The link from the parent to a child is reflected
in the ref_count of the child, but the link from the child to the
parent is not reflected in the ref_count of the parent.
Like a GtkObject, a GtkWidget is created with a ref_count of 1 and
initially flagged as `floating'. As soon as it is added as a child to
a parent, the `floating' flag is cleared and never will be set again.
Not even when it is later unparented. The act of clearing the
`floating' flag also decrements the ref_count of the widget by one.
When the widget is unparented, its underlying GdkWindow is destroyed
(when it has one), it loses its reference from the parent and
naturally the ref_count is decremented.
It is considered a bug if a widget still has a GdkWindow when it is
being freed.
Toplevel widgets, which don't have a `natural' parent, are adopted by
a special widget, maybe a GtkDisplay or GtkScreen. This special
parent of all toplevel widgets is never freed. The toplevel widgets
are added to this parent as soon as they are created. [Maybe this
special widget will only exist conceptually because toplevel widgets
are identified by parent == NULL through-out the code.]
So, the typical career of a GtkWindow and the GtkButton that sits in
it looks like this:
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
// window is created with ref_count == 1. It is not flagged as
// `floating' because it has already been added to the special
// parent of all toplevel widgets.
button = gtk_button_new_with_label ("Yo!");
// button->ref_count == 1 and it is flagged as `floating'.
gtk_container_add (window, button);
// button->ref_count still == 1, but it is no longer `floating'.
gtk_widget_show (button);
gtk_widget_show (window);
// The widgets get their GdkWindows, nothing significant happens to
// the ref_counts.
Then, when the user wants to get rid of the window:
gtk_widget_destroy (window);
// The GdkWindow of `window' and all its child GdkWindows are
// destroyed.
// window is removed from its (conceptual) parent and its ref_count
// drops to zero. The destroy code of `window' destroyes `button'.
// The destriction of the button removes it from its parent, the
// button->ref_count drops to zero and the button is freed, too.
- Marius Vollmer <>
......@@ -300,6 +300,8 @@ GdkGC* gdk_gc_new (GdkWindow *window);
GdkGC* gdk_gc_new_with_values (GdkWindow *window,
GdkGCValues *values,
GdkGCValuesMask values_mask);
GdkGC* gdk_gc_ref (GdkGC *gc);
void gdk_gc_unref (GdkGC *gc);
void gdk_gc_destroy (GdkGC *gc);
void gdk_gc_get_values (GdkGC *gc,
GdkGCValues *values);
......@@ -50,6 +50,7 @@ gdk_gc_new_with_values (GdkWindow *window,
xwindow = window_private->xwindow;
private->xdisplay = window_private->xdisplay;
private->ref_count = 1;
xvalues.function = GXcopy;
xvalues.fill_style = FillSolid;
......@@ -220,15 +221,35 @@ gdk_gc_new_with_values (GdkWindow *window,
gdk_gc_destroy (GdkGC *gc)
GdkGCPrivate *private;
gdk_gc_unref (gc);
g_return_if_fail (gc != NULL);
GdkGC *
gdk_gc_ref (GdkGC *gc)
GdkGCPrivate *private = (GdkGCPrivate*) gc;
private = (GdkGCPrivate*) gc;
XFreeGC (private->xdisplay, private->xgc);
g_return_val_if_fail (gc != NULL, NULL);
private->ref_count += 1;
return gc;
memset (gc, 0, sizeof (GdkGCPrivate));
g_free (gc);
gdk_gc_unref (GdkGC *gc)
GdkGCPrivate *private = (GdkGCPrivate*) gc;
g_return_if_fail (gc != NULL);
if (private->ref_count > 1)
private->ref_count -= 1;
XFreeGC (private->xdisplay, private->xgc);
memset (gc, 0, sizeof (GdkGCPrivate));
g_free (gc);
......@@ -58,8 +58,8 @@ struct _GdkWindowPrivate
guint16 width;
guint16 height;
guint8 resize_count;
guint8 ref_count;
guint8 window_type;
guint ref_count;
guint destroyed : 2;
guint dnd_drag_enabled : 1,
dnd_drag_datashow : 1,
......@@ -104,6 +104,7 @@ struct _GdkGCPrivate
GdkGC gc;
GC xgc;
Display *xdisplay;
guint ref_count;
struct _GdkColormapPrivate
......@@ -114,7 +115,7 @@ struct _GdkColormapPrivate
GdkVisual *visual;
gint private_val;
gint next_color;
gint ref_count;
guint ref_count;
struct _GdkVisualPrivate
......@@ -130,7 +131,7 @@ struct _GdkFontPrivate
/* generic pointer point to XFontStruct or XFontSet */
gpointer xfont;
Display *xdisplay;
gint ref_count;
guint ref_count;
struct _GdkCursorPrivate
......@@ -117,3 +117,15 @@ gtk_adjustment_new (gfloat value,
return GTK_OBJECT (adjustment);
gtk_adjustment_set_value (GtkAdjustment *adjustment,
gfloat value)
g_return_if_fail (adjustment != NULL);
g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
adjustment->value = CLAMP (value, adjustment->lower, adjustment->upper);
gtk_signal_emit_by_name (GTK_OBJECT (adjustment), "value_changed");
......@@ -64,6 +64,10 @@ GtkObject* gtk_adjustment_new (gfloat value,
gfloat step_increment,
gfloat page_increment,
gfloat page_size);
void gtk_adjustment_set_value (GtkAdjustment *adjustment,
gfloat value);
#ifdef __cplusplus
......@@ -20,7 +20,6 @@
static void gtk_bin_class_init (GtkBinClass *klass);
static void gtk_bin_init (GtkBin *bin);
static void gtk_bin_destroy (GtkObject *object);
static void gtk_bin_map (GtkWidget *widget);
static void gtk_bin_unmap (GtkWidget *widget);
static void gtk_bin_draw (GtkWidget *widget,
......@@ -76,8 +75,6 @@ gtk_bin_class_init (GtkBinClass *class)
parent_class = gtk_type_class (gtk_container_get_type ());
object_class->destroy = gtk_bin_destroy;
widget_class->map = gtk_bin_map;
widget_class->unmap = gtk_bin_unmap;
widget_class->draw = gtk_bin_draw;
......@@ -97,27 +94,6 @@ gtk_bin_init (GtkBin *bin)
static void
gtk_bin_destroy (GtkObject *object)
GtkBin *bin;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_IS_BIN (object));
bin = GTK_BIN (object);
if (bin->child)
bin->child->parent = NULL;
gtk_object_unref (GTK_OBJECT (bin->child));
gtk_widget_destroy (bin->child);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
static void
gtk_bin_map (GtkWidget *widget)
......@@ -261,7 +237,6 @@ gtk_bin_remove (GtkContainer *container,
if (bin->child == widget)
gtk_widget_unparent (widget);
bin->child = NULL;
if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (container))
......@@ -31,7 +31,6 @@ static void gtk_box_get_arg (GtkBox *box,
static void gtk_box_set_arg (GtkBox *box,
GtkArg *arg,
guint arg_id);
static void gtk_box_destroy (GtkObject *object);
static void gtk_box_map (GtkWidget *widget);
static void gtk_box_unmap (GtkWidget *widget);
static void gtk_box_draw (GtkWidget *widget,
......@@ -90,8 +89,6 @@ gtk_box_class_init (GtkBoxClass *class)
gtk_object_add_arg_type ("GtkBox::spacing", GTK_TYPE_INT, ARG_SPACING);
gtk_object_add_arg_type ("GtkBox::homogeneous", GTK_TYPE_BOOL, ARG_HOMOGENEOUS);
object_class->destroy = gtk_box_destroy;
widget_class->map = gtk_box_map;
widget_class->unmap = gtk_box_unmap;
widget_class->draw = gtk_box_draw;
......@@ -419,37 +416,6 @@ gtk_box_set_child_packing (GtkBox *box,
static void
gtk_box_destroy (GtkObject *object)
GtkBox *box;
GtkBoxChild *child;
GList *children;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_IS_BOX (object));
box = GTK_BOX (object);
children = box->children;
while (children)
child = children->data;
children = children->next;
child->widget->parent = NULL;
gtk_object_unref (GTK_OBJECT (child->widget));
gtk_widget_destroy (child->widget);
g_free (child);
g_list_free (box->children);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
static void
gtk_box_map (GtkWidget *widget)
......@@ -48,7 +48,6 @@ static void gtk_button_init (GtkButton *button);
static void gtk_button_set_arg (GtkButton *button,
GtkArg *arg,
guint arg_id);
static void gtk_button_destroy (GtkObject *object);
static void gtk_button_map (GtkWidget *widget);
static void gtk_button_unmap (GtkWidget *widget);
static void gtk_button_realize (GtkWidget *widget);
......@@ -170,8 +169,6 @@ gtk_button_class_init (GtkButtonClass *klass)
gtk_object_class_add_signals (object_class, button_signals, LAST_SIGNAL);
object_class->destroy = gtk_button_destroy;
widget_class->activate_signal = button_signals[CLICKED];
widget_class->map = gtk_button_map;
widget_class->unmap = gtk_button_unmap;
......@@ -223,7 +220,10 @@ gtk_button_set_arg (GtkButton *button,
gtk_container_disable_resize (GTK_CONTAINER (button));
if (button->child)
gtk_widget_destroy (button->child);
gtk_widget_unparent (button->child);
button->child = NULL;
label = gtk_label_new (GTK_VALUE_STRING(*arg));
gtk_widget_show (label);
......@@ -289,27 +289,6 @@ gtk_button_leave (GtkButton *button)
gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
static void
gtk_button_destroy (GtkObject *object)
GtkButton *button;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_IS_BUTTON (object));
button = GTK_BUTTON (object);
if (button->child)
button->child->parent = NULL;
gtk_object_unref (GTK_OBJECT (button->child));
gtk_widget_destroy (button->child);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
static void
gtk_button_map (GtkWidget *widget)
......@@ -109,7 +109,7 @@ static void gtk_color_selection_rgb_updater (GtkWidget *widget,
static void gtk_color_selection_opacity_updater (GtkWidget *widget,
gpointer data);
static void gtk_color_selection_realize (GtkWidget *widget);
static void gtk_color_selection_destroy (GtkObject *object);
static void gtk_color_selection_finalize (GtkObject *object);
static void gtk_color_selection_color_changed (GtkColorSelection *colorsel);
static void gtk_color_selection_update_input (GtkWidget *scale,
GtkWidget *entry,
......@@ -159,8 +159,6 @@ static void gtk_color_selection_hsv_to_rgb (gdouble h, gdouble s, gdoub
static void gtk_color_selection_rgb_to_hsv (gdouble r, gdouble g, gdouble b,
gdouble *h, gdouble *s, gdouble *v);
static void gtk_color_selection_dialog_destroy (GtkObject *object);
static GtkVBoxClass *color_selection_parent_class = NULL;
static GtkWindowClass *color_selection_dialog_parent_class = NULL;
......@@ -229,7 +227,7 @@ gtk_color_selection_class_init (GtkColorSelectionClass *klass)
gtk_object_class_add_signals (object_class, color_selection_signals, LAST_SIGNAL);
object_class->destroy = gtk_color_selection_destroy;
object_class->finalize = gtk_color_selection_finalize;
widget_class->realize = gtk_color_selection_realize;
......@@ -515,7 +513,7 @@ gtk_color_selection_realize (GtkWidget *widget)
static void
gtk_color_selection_destroy (GtkObject *object)
gtk_color_selection_finalize (GtkObject *object)
GtkColorSelection *colorsel;
......@@ -531,8 +529,7 @@ gtk_color_selection_destroy (GtkObject *object)
if (colorsel->sample_buf != NULL)
g_free (colorsel->sample_buf);
if (GTK_OBJECT_CLASS (color_selection_parent_class)->destroy)
(*GTK_OBJECT_CLASS (color_selection_parent_class)->destroy) (object);
(*GTK_OBJECT_CLASS (color_selection_parent_class)->finalize) (object);
static void
......@@ -1423,8 +1420,6 @@ gtk_color_selection_dialog_class_init (GtkColorSelectionDialogClass *klass)
object_class = (GtkObjectClass*) klass;
color_selection_dialog_parent_class = gtk_type_class (gtk_window_get_type ());
object_class->destroy = gtk_color_selection_dialog_destroy;
static void
......@@ -1477,18 +1472,3 @@ gtk_color_selection_dialog_new (const gchar *title)
return GTK_WIDGET (colorseldiag);
static void
gtk_color_selection_dialog_destroy (GtkObject *object)
GtkColorSelectionDialog *colorseldiag;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_IS_COLOR_SELECTION_DIALOG (object));
colorseldiag = GTK_COLOR_SELECTION_DIALOG (object);
if (GTK_OBJECT_CLASS (color_selection_dialog_parent_class)->destroy)
(*GTK_OBJECT_CLASS (color_selection_dialog_parent_class)->destroy) (object);
......@@ -71,6 +71,7 @@ static void gtk_container_marshal_signal_4 (GtkObject *object,
static void gtk_container_class_init (GtkContainerClass *klass);
static void gtk_container_init (GtkContainer *container);
static void gtk_container_destroy (GtkObject *object);
static void gtk_container_get_arg (GtkContainer *container,
GtkArg *arg,
guint arg_id);
......@@ -101,6 +102,7 @@ static void gtk_container_hide_all (GtkWidget *widget);
static gint container_signals[LAST_SIGNAL] = { 0 };
static GtkWidgetClass *parent_class = NULL;
gtk_container_get_type ()
......@@ -134,7 +136,8 @@ gtk_container_class_init (GtkContainerClass *class)
object_class = (GtkObjectClass*) class;
widget_class = (GtkWidgetClass*) class;
parent_class = gtk_type_class (gtk_widget_get_type ());
gtk_object_add_arg_type ("GtkContainer::border_width", GTK_TYPE_LONG, ARG_BORDER_WIDTH);
gtk_object_add_arg_type ("GtkContainer::auto_resize", GTK_TYPE_BOOL, ARG_AUTO_RESIZE);
gtk_object_add_arg_type ("GtkContainer::block_resize", GTK_TYPE_BOOL, ARG_BLOCK_RESIZE);
......@@ -182,10 +185,12 @@ gtk_container_class_init (GtkContainerClass *class)
gtk_object_class_add_signals (object_class, container_signals, LAST_SIGNAL);
object_class->destroy = gtk_container_destroy;
/* Other container classes should overwrite show_all and hide_all,
unless they make all their children accessable
through gtk_container_foreach.
* for the purpose of showing internal children also, which are not
* accessable through gtk_container_foreach.
widget_class->show_all = gtk_container_show_all;
widget_class->hide_all = gtk_container_hide_all;
......@@ -202,6 +207,29 @@ gtk_container_init (GtkContainer *container)
container->auto_resize = TRUE;
container->need_resize = FALSE;
container->block_resize = FALSE;
container->resize_widgets = NULL;
static void
gtk_container_destroy (GtkObject *object)
GSList *node;
for (node = GTK_CONTAINER (object)->resize_widgets; node; node = node->next)
GtkWidget *child;
child = (GtkWidget*) node->data;
g_slist_free (GTK_CONTAINER (object)->resize_widgets);
GTK_CONTAINER (object)->resize_widgets = NULL;
gtk_container_foreach (GTK_CONTAINER (object),
(GtkCallback) gtk_widget_destroy, NULL);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
static void
......@@ -330,7 +358,7 @@ gtk_container_unblock_resize (GtkContainer *container)
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_CONTAINER (container));
container->block_resize = FALSE;
......@@ -338,12 +366,12 @@ gint
gtk_container_need_resize (GtkContainer *container)
gint return_val;
g_return_val_if_fail (container != NULL, FALSE);
g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
return_val = FALSE;
if (!container->block_resize)
if (container->auto_resize)
......@@ -353,7 +381,7 @@ gtk_container_need_resize (GtkContainer *container)
container->need_resize = TRUE;
return return_val;
......@@ -367,12 +395,56 @@ gtk_container_foreach (GtkContainer *container,
callback, callback_data);
typedef struct _GtkForeachData GtkForeachData;
struct _GtkForeachData
GtkObject *container;
GtkCallbackMarshal callback;
gpointer callback_data;
static void
gtk_container_foreach_unmarshal (GtkWidget *child,
gpointer data)
GtkForeachData *fdata = (GtkForeachData*) data;
GtkArg args[2];
/* first argument */
args[0].name = NULL;
args[0].type = GTK_OBJECT(child)->klass->type;
GTK_VALUE_OBJECT(args[0]) = GTK_OBJECT (child);
/* location for return value */
args[1].name = NULL;
args[1].type = GTK_TYPE_NONE;
fdata->callback (fdata->container, fdata->callback_data, 1, args);
gtk_container_foreach_interp (GtkContainer *container,
GtkCallbackMarshal callback,
gpointer callback_data,
GtkDestroyNotify notify)
GtkForeachData fdata;
fdata.container = GTK_OBJECT (container);
fdata.callback = callback;
fdata.callback_data = callback_data;