Re: Memory problem



On Thu, 14 Sep 2000, Antony Stace wrote:

> 
> Hi Folks
> 
> I have a function which is eating up lots of memory.  The function is
> listed below and is called every 1 second with the same
> 
> GtkWidget *widget
> 
> being passed to it each time.
> What do I need to do to stop it eating up memory.  All I want the
> function to do is set the colours of itself and its parent(if it has
> one).
> Any help greatly appreciated.
> 
> Cheers
> Tony
> 
> 
> 
> 
> int SetWidgetColour(GtkWidget *widget, int bordersize, int red,
>                      int green, int blue)
> {
>     GtkStyle *new_style;
>     GdkColor color;
> 
>     color.red = red;
>     color.green = green;
>     color.blue = blue;
> 
>     gdk_color_alloc(gdk_colormap_get_system(), &color);
>     new_style = gtk_style_copy(gtk_widget_get_default_style());
>     new_style->bg[GTK_STATE_NORMAL] = color;
>     new_style->bg[GTK_STATE_PRELIGHT] = color;
>     new_style->bg[GTK_STATE_ACTIVE] = color;
>     gtk_style_detach (widget->style);
this line shouldn't be necessary.

>     gtk_widget_set_style(GTK_WIDGET(widget), new_style);

You are leaking new_style with each call to this function.  When you
create new_style, it has a reference count of 1.  When you call
..._set_style(), its reference count is incremented to 2.  When you set a
new style on widget, the reference count goes back to 1, so is never
destroyed.

To fix the problem, add the following statement after set_style():
      gtk_style_unref(new_style);

>   
>     return TRUE;
> }
> 

James.

-- 
Email: james daa com au
WWW:   http://www.daa.com.au/~james/






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