Re: leak in widget new/destroy
- From: Owen Taylor <otaylor redhat com>
- To: Stephane Chauveau <s chauveau chello nl>
- Cc: gtk-devel-list gnome org
- Subject: Re: leak in widget new/destroy
- Date: Sun, 3 Nov 2002 15:51:51 -0500 (EST)
Stephane Chauveau <s chauveau chello nl> writes:
> Hello,
>
> I was looking for memory leaks in my program and I found that memory
> seems to be
> leaked after allocating and destroying widgets such as GtkButton and
> GtkMenu
> with Gtk 2.1.0
>
> The example program below, allocates and frees a large number of
> GtkButton.
> It shows that around 80 bytes are leaked by each new/destroy. In
> fact, gtk_widget_destroy does not seem to free anything.
What you are missing is that gtk_widget_destroy() doesn't do
memory management ... what does is basically tell everybody
"I want this widget to go away, stop holding onto references
to it".
The reason you think that it has something to do with memory
management is two particular things that happen:
- Toplevel windows are owned by GTK+ when initially created.
When _destroy() is called on them, GTK+ drops this reference.
- Other widgets are removed from their parent, so the reference
that their parent owns to them is removed.
But when you call gtk_button_new() nobody owns the button
until someone "adopts" the initial reference, so nobody
is going to drop the reference in response to destroy().
Generally, if you want to have a widget that you may or
not add to a parent, the thing to do is:
When you create the widget:
b = gtk_button_new ();
/* Adopt the initial reference */
g_object_ref (G_OBJECT (b));
gtk_object_sink (GTK_OBJECT (b));
When you want to get rid of the widget:
/* Tell other people to drop their references */
gtk_object_destroy (GTK_OBJECT (b));
/* Drop your own reference */;
g_object_unref (GTK_OBJECT (b));
Regards,
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]