RE: [gtk-list] Re: style usage + memory leak



Robert Roebling wrote:

> No. When I create the style, its ref count is 1. When I then
> assign it to a widget, I assume the widgets will raise the
> ref count to 2. Without unref after assigning it the widget
> would unref the style upon its own destruction leaving
> the ref count at 1 -> memory leak.

I thought that when you created something in Gtk+, it was "floating," with
no reference count, until you assigned it to something.  So when you create
the style, it is "floating" (ref count 0), until you assign it to a widget,
raising the ref count to 1.  The danger zone is when
an object is "floating," the interval between when it is created and when it
is assigned to something.  If you create the style and don't assign it to
any widget, then you would have a memory leak.

This is different than the way one would implement reference counting in
C++.   In C++, an object's destructor gets called when the object goes out
of scope, so you would set the reference count to 1 when the object is
created (in the constructor), and decrement the ref count in the destructor.
In C, of course, nothing special happens when an object goes out of scope
(there is no destructor), so you don't set the reference count to 1 when the
object is created because if you did, it would never go back to zero
(without explicit programmer intervention, which would defeat the whole
purpose of automatic garbage collection).  Thus the "floating" danger-zone
concept.  The C++ approach is safer and clearer, of course, but the C
approach does work.

Steve Molitor
smolitor@insight-tech.com


-----Original Message-----
From: karl@mail.redhat.com [mailto:karl@mail.redhat.com]On Behalf Of
Robert Roebling
Sent: Thursday, October 29, 1998 10:25 AM
To: gtk-list@redhat.com
Subject: [gtk-list] Re: style usage + memory leak


Eric Harlow wrote:

> You should not be calling unref unless you are also making the ref
> call.  If you just call the unref (as in your example), this should
> end up freeing the style you just created.

No. When I create the style, its ref count is 1. When I then
assign it to a widget, I assume the widgets will raise the
ref count to 2. Without unref after assigning it the widget
would unref the style upon its own destruction leaving
the ref count at 1 -> memory leak.

> GTK keeps track of memory pretty well and you should rely on GTK
> to do that work for you.

Daring thesis. Also GTK+ cannot handle memory on its
own, you'd still have to know when to unref unless you
use C++ which can do that itself. wxWindows does it for
you - as an aside...

And I'm still wondering about the 1100 memory leaks reported
by dmalloc. It could be a cashing function. I hope it is.

  Robert

--
------------------------------------------------------------------
Robert Roebling              "Write once, compile, run everywhere"

wxWindows/GTK       http://wesley.informatik.uni-freiburg.de/~wxxt

--
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null



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