[patch]: another-clock leak fixes



Hi there...

This patch fixes some memory leaks in the another-clock applet in
gnome-applets. (at least, all the ones detected by memprof).
Specifically:
* GCs are now no longer leaked when using the preference dialog box
* Color strings are also no longer leaked.

=)
Peter

PS, is this the right place to send this?
Index: another_clock.c
===================================================================
RCS file: /cvs/gnome/gnome-applets/another_clock/another_clock.c,v
retrieving revision 1.8
diff -u -r1.8 another_clock.c
--- another_clock.c	2000/01/28 21:20:17	1.8
+++ another_clock.c	2000/02/12 03:53:53
@@ -117,7 +117,22 @@
 
 static void props_ok (GtkWidget *wid, int page, gpointer *data)
 {
-    memcpy (&clk.props, &props_tmp, sizeof(struct clock_props_t));
+    /* Free the existing property strings */
+    if (clk.props.bg)
+        g_free( clk.props.bg ); 
+    if (clk.props.min)
+        g_free( clk.props.min ); 
+    if (clk.props.hour)
+        g_free( clk.props.hour ); 
+    if (clk.props.sec)
+        g_free( clk.props.sec ); 
+    /* Copy the new properties structure over */
+    clk.props.secneedle = props_tmp.secneedle;
+    clk.props.bg = g_strdup(props_tmp.bg);
+    clk.props.hour = g_strdup(props_tmp.hour);
+    clk.props.min = g_strdup(props_tmp.min);
+    clk.props.sec = g_strdup(props_tmp.sec);
+
     applet_widget_sync_config (APPLET_WIDGET(applet));
     set_colors(clk.area);
     update_clock (NULL);
@@ -131,6 +146,16 @@
 static void props_cancel (GtkWidget *widget, GtkWidget **win)
 {
     *win = NULL;
+    /* Free the strings used in props_tmp */
+    if ( props_tmp.bg != NULL )
+        g_free(props_tmp.bg);
+    if ( props_tmp.hour != NULL )
+        g_free(props_tmp.hour);
+    if ( props_tmp.min != NULL )
+        g_free(props_tmp.min);
+    if ( props_tmp.sec != NULL )
+        g_free(props_tmp.sec);
+
     return;
     widget = NULL;
 }
@@ -139,11 +164,11 @@
 static void bg_color_changed (GnomeColorPicker *cp)
 {
     guint8 r, g, b;
-    gchar  buf[24];
 		
     gnome_color_picker_get_i8 (cp, &r, &g, &b, NULL);
-    sprintf (buf, "#%02x%02x%02x", r, g, b);
-    strcpy (props_tmp.bg, buf);
+    if (props_tmp.bg != NULL)
+        g_free( props_tmp.bg );
+    props_tmp.bg = g_strdup_printf( "#%02x%02x%02x", r, g, b);
     gnome_property_box_changed (GNOME_PROPERTY_BOX(props_window));
 }
 
@@ -151,11 +176,11 @@
 static void hour_color_changed (GnomeColorPicker *cp)
 {
     guint8 r, g, b;
-    gchar  buf[24];
 		
     gnome_color_picker_get_i8 (cp, &r, &g, &b, NULL);
-    sprintf (buf, "#%02x%02x%02x", r, g, b);
-    strcpy (props_tmp.hour, buf);
+    if (props_tmp.hour != NULL)
+        g_free( props_tmp.hour );
+    props_tmp.hour = g_strdup_printf( "#%02x%02x%02x", r, g, b);
     gnome_property_box_changed (GNOME_PROPERTY_BOX(props_window));
 }
 
@@ -163,11 +188,11 @@
 static void min_color_changed (GnomeColorPicker *cp)
 {
     guint8 r, g, b;
-    gchar  buf[24];
 		
     gnome_color_picker_get_i8 (cp, &r, &g, &b, NULL);
-    sprintf (buf, "#%02x%02x%02x", r, g, b);
-    strcpy (props_tmp.min, buf);
+    if (props_tmp.min != NULL)
+        g_free( props_tmp.min );
+    props_tmp.min = g_strdup_printf( "#%02x%02x%02x", r, g, b);
     gnome_property_box_changed (GNOME_PROPERTY_BOX(props_window));
 }
 
@@ -175,11 +200,11 @@
 static void sec_color_changed (GnomeColorPicker *cp)
 {
     guint8 r, g, b;
-    gchar  buf[24];
 		
     gnome_color_picker_get_i8 (cp, &r, &g, &b, NULL);
-    sprintf (buf, "#%02x%02x%02x", r, g, b);
-    strcpy (props_tmp.sec, buf);
+    if (props_tmp.sec != NULL)
+        g_free( props_tmp.sec );
+    props_tmp.sec = g_strdup_printf( "#%02x%02x%02x", r, g, b);
     gnome_property_box_changed (GNOME_PROPERTY_BOX(props_window));
 }
 
@@ -208,6 +233,13 @@
     GtkWidget *colorpicker;
     guint     r, g, b;
 
+    /* If there's already a property window around, raise it */
+    if (props_window)
+    {
+       gdk_window_raise (props_window->window);
+       return;
+    }
+
     /* init temporal properties */
     props_tmp.secneedle = clk.props.secneedle;
     props_tmp.bg = g_strdup (clk.props.bg);
@@ -217,12 +249,6 @@
 
     help_entry.name = gnome_app_id;
 
-    if (props_window)
-    {
-       gdk_window_raise (props_window->window);
-       return;
-    }
-																			 
     /* Window and frame for settings */
     props_window = gnome_property_box_new ();
     gtk_window_set_title (GTK_WINDOW(&GNOME_PROPERTY_BOX(props_window)->dialog.window),
@@ -552,13 +578,19 @@
 static void set_colors (GtkWidget *widget)
 {
     GdkColorContext *cc;
+    int i;
 
     cc = gdk_color_context_new (gtk_widget_get_visual(widget),
                                 gtk_widget_get_colormap(widget));
-    clk.gc[0] = gdk_gc_new (widget->window);
-    clk.gc[1] = gdk_gc_new (widget->window);
-    clk.gc[2] = gdk_gc_new (widget->window);
-    clk.gc[3] = gdk_gc_new (widget->window);
+    for (i=0; i<4; i++)
+    {
+        /* Free any existing gc's */
+        if (clk.gc[i] != NULL)
+        {
+            gdk_gc_unref(clk.gc[i]);
+        }
+        clk.gc[i] = gdk_gc_new (widget->window);
+    }
     set_gc_color (cc, 0);
     set_gc_color (cc, 1);
     set_gc_color (cc, 2);
@@ -629,6 +661,9 @@
 int main (int argc, char *argv[])
 {
     char *fname;
+
+    /* Zero the clock structure */
+    memset(&clk, 0, sizeof(clk));
 
     /* Initialize the i18n stuff */
     bindtextdomain (PACKAGE, GNOMELOCALEDIR);


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