Calin Vatavu wrote:
> 
> > The  function works fine except it eats some memory every it is called (I
> > can monitor this with top command).
> > Do I have to to a free (style) ???
> >
> 
> Yes, you have to free it (with g_free I believe), but it's better
> to make it static:
> ..
>       static GtkStyle       * style=0;
> 
>       if (!style) {style=gtk_style_new();}
> ..
> It's faster, but non-reentrant
> 
It also does nothing to solve the memory leak :)

Specifically it isn't the pointer that was leaking, it was the memory
being pointed at.  The pointer itself is an automatic variable (you've
moved it to .bss), and as such is allocated on the stack on function
entry.  It is automatically dealocated when the stack is unrolled on
return.

What is happening is that the gtk_style_new function is allocating a new
GtkStyle structure on the heap; incrementing its reference count
(ref=1); and returning a pointer to the new struct which you store in
`style'.

When you call gtk_widget_set_style(...) the widget stores the GtkStyle's
address; increments it's reference count (ref=2); and then applies the
style.

You are then allocating a new GtkStyle (ref=1).  Calling _set_style()
which decrements the reference count of the old GtkStyle(ref=1), and
proceeds as above.

Note that dispite your pointer no longer holding a reference to the
GtkStyle, it never releases it's (shared) ownership of it.  This means
that when the widget finally releases it's (shared) ownership the
GtkStyle is not deallocated.

The solution is to release ownership of the GtkStyle after passing it to
the widget. 

ie.

gtk_widget_set_style(entry, style);
gtk_object_unref(GTK_OBJECT(style));

This will mean that when the widget decrements the reference count it
will reach 0 (rather then 1) and the GtkStyle will be freed.

Andrae

P.S.  Note that discussions like this should really be on glade-users
rather then glade-devel, which is supposed to be for discussing the
continuing development of glade rather then coding questions about its
use.

_______________________________________________
Glade-devel maillist  -  [EMAIL PROTECTED]
http://lists.helixcode.com/mailman/listinfo/glade-devel

Reply via email to