Re: memory leaks destroying widgets
- From: Owen Taylor <otaylor redhat com>
- To: gtk-devel-list redhat com
- Subject: Re: memory leaks destroying widgets
- Date: 30 Aug 1999 11:15:16 -0400
Owen Taylor <otaylor@redhat.com> writes:
> judd montgomery <judd@engineer.com> writes:
>
> > I have an application in which I create lots of widgets. clists, text
> > boxes, entries, etc. Then I destroy the box that they are all packed
> > into using gtk_widget_destroy() and then I redraw more widgets, and then
> > I repeat this process. Why do I have memory leaks? Should I be doing
> > something more?
>
> That should work. Without more information it is hard
> to say why you are leaking memory.
>
> (When I say that it works, it works assuming that box
> is packed into a toplevel.
>
> box = gtk_hbox_new (FALSE, 0);
> label = gtk_label_new ("Hi");
> gtk_container_add (GTK_CONTAINER (box), label);
> gtk_widget_destroy (box);
>
> Leaks both the box and the label.
>
> A widget is created with a reference count of 1; this
> reference count is taken over by a parent when you add
> it to a parent.)
I was asked for some more information on this.
The detailed source of information on memory management
issues is docs/refcounting.txt; I'll just summarize
briefly here.
The basic idea is that a widget is created in a special
FLOATING state with a reference count of 1. The FLOATING
flag means that the initial reference count is
"available for adoption". When a widget is added to
a container, the container checks the FLOATING flag,
and if it is set, takes over the reference count and
clears the flag (this involves calling gtk_widget_sink().)
In the above example, the FLOATING flag is still set
on 'box' when it is destroyed, and its reference
count remains at 1, so box is not destroyed.
(Actually, I was wrong when I said it leaks both the
box and the label; the memory for the label does
get freed).
Toplevel widgets, such as GtkWindows, are special in that
they automatically sink themselves. For a toplevel
window, calling gtk_widget_destroy() causes the
initial reference count to be released.
For more information, take a look at docs/refcounting.txt
or Havoc Pennington has information about this
in his book _GTK+/Gnome Application Development_.
Regards,
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]