Re: RFC: Reference counts on Gtk2::Tooltips



Andrew Pimlott said:
On Mon, May 05, 2003 at 11:40:31AM -0400, muppet wrote:
this happens because although the tooltips object stores a pointer to
itself in the client widget's user data, it does not allow that pointer to
keep a reference.  thus, it is up to the application programmer to store a
reference to the tooltips object which will outlive the objects which have
the tooltips attached to them.

Is there any chance of fixing gtk?

i wasn't sure whether it was a proper bug.  in looking at the Gtk source,
gtk_tooltips_set_tip calls a helper function which stores a structure in the
widget's user_data; that structure contains a pointer to the tooltips object. 
ostensibly this pointer should own a reference to the object, but currently it
does not, hence the behavior we see.

the GtkSizeGroup does a similar thing, but each client widget holds a
reference on the size group object.


however, that behavior is rather surprising.  also, from what i can tell
it is unexpected, since most C code i've seen actually leaks the tooltips
objects so they stay alive by accident.

So if you preserve the C behavior in Perl, how could the Perl
programmer avoid leaking tooltips?  Basically, you're saying that
every Perl programmer that doesn't want a memory leak has to invent his own
way of managing tooltips.  That seems like a pointless waste of effort.

well, there's not really a way to leak the tooltips reference in perl, because
the wrapper will clean up the object when the time comes.  in C, this object
is leaked simply by virtue of not calling _unref on it.

all you really have to do is keep the perl scalar around, either by adding it
to a hash or setting it as the user_data of some GObject.  having said that,
i've been writing code around this today and the bookkeeping gets a little
obnoxious.


it is trivial to have the perl wrapper for set_tip keep a reference on the
tooltips object so that it will stay alive; my question is, since it's
behaving as designed, is this subversion the right thing to do?  or should
we simply document the behavior in a "pitfalls" or "gotchas" document?

Does the C API have the hooks you would need to dereference the
tooltips when the widget is destroyed?

absolutely.  in fact, it's a two-statement workaround in XS:

      g_object_ref (G_OBJECT (tooltips));
      g_object_set_data_full (G_OBJECT (widget), "_gtk2perl_tooltips",
                              tooltips, (GDestroyNotify) g_object_unref);

i'm just waiting on the list to decide whether we should correct this or not.


In general, documenting pitfalls is stupid.

point taken.  in fact, i'd been using gtk-perl for several months before i
realized that there were manpages for it, and that my distro's package simply
didn't include them.


-- 
muppet <scott at asofyet dot org>





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