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




GTK keeps track of memory pretty well and you should rely on GTK to do
that work for you.  It uses references to keep track of items like
styles.

For instance, if you do a gtk_widget_set_style, there is a call to add
a reference to the style with gtk_style_ref.  If the widget is
destroyed or a new style is assigned to the widget, a gtk_style_unref
call is made which frees the style when the reference count is zero. 
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 - or at the minimum have the
widget's style pointer pointing to a block of now freed memory which
can be used by other memory allocations and will eventually cause a
crash - one that would be hard to track down. 

Of course, I didn't write this code, I'm just trying to understand it
so I can write about it.  If I'm wrong about this, can someone let me
know as I'm starting to document this now.

 -Eric

---Robert Roebling <roebling@sun2.ruf.uni-freiburg.de> wrote:
>
> Eric Harlow wrote:
> 
> >    /* --- Setup the colors first. --- */
> >    style->bg[GTK_STATE_NORMAL] = color;
> >    style->fg[GTK_STATE_NORMAL] = color;
> > 
> >    /* --- Then set the style --- */
> >    gtk_widget_set_style (button, style);
> 
> Thanks, I also wondered if that was the cause of my
> segfaults. There is one more aspect left, which is
> the question of memory leaks. What I now do is to
> unref the style directly after assigning it as I assume
> the widget to ref the style when using it. I don't
> get any segfaults with this but could anyone confirm
> that this is correct:
> 
>   mystyle = gtk_style_copy( gtk_widget_get_style( button ));
> 
>   /* change style here */
> 
>   gtk_widget_set_style( button, mystyle );
> 
>   gtk_style_unref( mystyle );
> 
> If I don't unref the style, then the mystyle reference will
> get lost and the style cannot get disposed of later on, right?
> 
> This leads me to the general question of how to detect
> memory leaks in GTK+. I ran dmalloc on the button sample
> in GTK+ and it reports 980 memory holes. Do the same with
> testgtk and you up at 1100, use testgtk by clicking on
> it a few times here and there and the leak climbs up
> (although I seem to remember that you get maximum and
> then no more - could be wrong). How can I detect memory
> holes? I have little understanding of GTK+ built-in
> cashing system which probably must create memory holes
> in the sense of what dmalloc and friends report. Given
> that some of GTK+'s memory leaks have been found only
> very recently, I assume that it is impossible to have
> any kind of automated detection.
> 
>   Thanks,
> 
>     Robert
> 
> -- 
> ----------------------------------------------------------------
> Robert Roebling   http://wesley.informatik.uni-freiburg.de/~wxxt
> 
> -- 
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com <
/dev/null
> 
> 

_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com



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