Re: gobject reference question



On Mon, 2005-08-15 at 12:42 -0700, Wallace Owen wrote:
On Mon, 2005-08-15 at 16:29 -0400, Tristan Van Berkom wrote:
Wallace Owen wrote:
How can I see what the current reference count is, so I can be sure I'm
using g_object_ref() and g_object_unref() correctly?

You can check G_OBJECT (object)->ref_count, note that it is
fundementally wrong to do any conditional code based on this
(for the same reason you cant check if any free'd memory is
actually free'd) but it may help you debug.

Yow!  I guess I was going to use it incorrectly.  I had searched through
the demos for code that called g_object_unref(), and searched the API
docs installed on my machine, and searched the tutorials on the GTK site
for clues about when to bump the reference count and when to decrement
it.  What I did not see was documentation telling me which gtk library
functions would increment the count.

I assumed, after my search, that putting a widget into a container would
increment it's reference count.  But no, adding code to show the
refcount after adding the widget to the container, that doesn't seem to
be the case.  Putting a table into a dialog didn't seem to increase the
table's refcount, and adding labels to the table didn't increase the
refcnt of either the table or the labels.


You probably want to read up on the concept of "floating" references,
which are what you are seeing here. When a widget is created, it has an 
initial "floating" reference. When you add it to a container while it is
still floating, the container takes over the floating reference instead
of adding a new one.




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