[gnome-power-manager] Use g_ptr_array_unref() in more places, which also fixes a few small memory leaks



commit 83186cc819802d8de11021673cec5c2ff5c1237b
Author: Richard Hughes <richard hughsie com>
Date:   Fri Sep 11 16:17:45 2009 +0100

    Use g_ptr_array_unref() in more places, which also fixes a few small memory leaks

 src/gpm-brightness-xrandr.c |   13 +++----------
 src/gpm-engine.c            |   23 ++++++-----------------
 src/gpm-engine.h            |    2 +-
 src/gpm-graph-widget.c      |   17 ++++-------------
 src/gpm-manager.c           |    4 ++--
 src/gpm-prefs-core.c        |    2 +-
 src/gpm-statistics.c        |   28 ++++++++++------------------
 src/gpm-tray-icon.c         |    4 ++--
 8 files changed, 29 insertions(+), 64 deletions(-)
---
diff --git a/src/gpm-brightness-xrandr.c b/src/gpm-brightness-xrandr.c
index 7cffe96..bcd026f 100644
--- a/src/gpm-brightness-xrandr.c
+++ b/src/gpm-brightness-xrandr.c
@@ -609,7 +609,6 @@ gpm_brightness_monitors_changed (GdkScreen *screen, GpmBrightnessXRandR *brightn
 static void
 gpm_brightness_xrandr_update_cache (GpmBrightnessXRandR *brightness)
 {
-	guint i;
 	guint length;
 	gint screen;
 	Window root;
@@ -621,13 +620,8 @@ gpm_brightness_xrandr_update_cache (GpmBrightnessXRandR *brightness)
 
 	/* invalidate and remove all the previous entries */
 	length = brightness->priv->resources->len;
-	for (i=0; i<length; i++) {
-		resource = (XRRScreenResources *) g_ptr_array_index (brightness->priv->resources, i);
-		egg_debug ("freeing resource %p", resource);
-		XRRFreeScreenResources (resource);
-	}
 	if (length > 0)
-		g_ptr_array_remove_range (brightness->priv->resources, 0, length);
+		g_ptr_array_set_size (brightness->priv->resources, 0);
 
 	/* do for each screen */
 	display = gdk_display_get_default ();
@@ -676,8 +670,7 @@ gpm_brightness_xrandr_finalize (GObject *object)
 	g_return_if_fail (GPM_IS_BRIGHTNESS_XRANDR (object));
 	brightness = GPM_BRIGHTNESS_XRANDR (object);
 
-	g_ptr_array_foreach (brightness->priv->resources, (GFunc) XRRFreeScreenResources, NULL);
-	g_ptr_array_free (brightness->priv->resources, FALSE);
+	g_ptr_array_unref (brightness->priv->resources);
 
 	G_OBJECT_CLASS (gpm_brightness_xrandr_parent_class)->finalize (object);
 }
@@ -716,7 +709,7 @@ gpm_brightness_xrandr_init (GpmBrightnessXRandR *brightness)
 
 	brightness->priv = GPM_BRIGHTNESS_XRANDR_GET_PRIVATE (brightness);
 	brightness->priv->hw_changed = FALSE;
-	brightness->priv->resources = g_ptr_array_new ();
+	brightness->priv->resources = g_ptr_array_new_with_free_func ((GDestroyNotify) XRRFreeScreenResources);
 
 	/* can we do this */
 	brightness->priv->has_extension = gpm_brightness_xrandr_setup_display (brightness);
diff --git a/src/gpm-engine.c b/src/gpm-engine.c
index cff507b..72fd0f0 100644
--- a/src/gpm-engine.c
+++ b/src/gpm-engine.c
@@ -874,7 +874,6 @@ gpm_engine_device_removed_cb (DkpClient *client, DkpDevice *device, GpmEngine *e
 	ret = g_ptr_array_remove (engine->priv->array, device);
 	if (!ret)
 		return;
-	g_object_unref (device);
 	gpm_engine_recalculate_state (engine);
 }
 
@@ -947,20 +946,13 @@ gpm_engine_device_changed_cb (DkpClient *client, DkpDevice *device, GpmEngine *e
 
 /**
  * gpm_engine_get_devices:
+ *
+ * Return value: the DkpDevice array, free with g_ptr_array_unref()
  **/
-const GPtrArray *
+GPtrArray *
 gpm_engine_get_devices (GpmEngine *engine)
 {
-	guint i;
-	DkpDevice *device;
-	GPtrArray *array = engine->priv->array;
-
-	for (i=0; i<array->len; i++) {
-		device = g_ptr_array_index (array, i);
-		g_object_ref (G_OBJECT(device));
-	}
-
-	return engine->priv->array;
+	return g_ptr_array_ref (engine->priv->array);
 }
 
 /**
@@ -1010,7 +1002,6 @@ phone_device_removed_cb (GpmPhone *phone, guint idx, GpmEngine *engine)
 
 		if (type == DKP_DEVICE_TYPE_PHONE) {
 			g_ptr_array_remove_index (engine->priv->array, i);
-			g_object_unref (device);
 			break;
 		}
 	}
@@ -1068,7 +1059,7 @@ gpm_engine_init (GpmEngine *engine)
 
 	engine->priv = GPM_ENGINE_GET_PRIVATE (engine);
 
-	engine->priv->array = g_ptr_array_new ();
+	engine->priv->array = g_ptr_array_new_with_free_func (g_object_unref);
 	engine->priv->client = dkp_client_new ();
 	g_signal_connect (engine->priv->client, "device-added",
 			  G_CALLBACK (gpm_engine_device_added_cb), engine);
@@ -1220,9 +1211,7 @@ gpm_engine_finalize (GObject *object)
 	engine = GPM_ENGINE (object);
 	engine->priv = GPM_ENGINE_GET_PRIVATE (engine);
 
-	g_ptr_array_foreach (engine->priv->array, (GFunc) g_object_unref, NULL);
-	g_ptr_array_free (engine->priv->array, TRUE);
-
+	g_ptr_array_unref (engine->priv->array);
 	g_object_unref (engine->priv->client);
 	g_object_unref (engine->priv->phone);
 	g_object_unref (engine->priv->battery_composite);
diff --git a/src/gpm-engine.h b/src/gpm-engine.h
index caaef13..57b300b 100644
--- a/src/gpm-engine.h
+++ b/src/gpm-engine.h
@@ -71,7 +71,7 @@ GType		 gpm_engine_get_type		(void);
 GpmEngine	*gpm_engine_new			(void);
 gchar		*gpm_engine_get_icon		(GpmEngine	*engine);
 gchar		*gpm_engine_get_summary		(GpmEngine	*engine);
-const GPtrArray	*gpm_engine_get_devices		(GpmEngine	*engine);
+GPtrArray	*gpm_engine_get_devices		(GpmEngine	*engine);
 
 G_END_DECLS
 
diff --git a/src/gpm-graph-widget.c b/src/gpm-graph-widget.c
index fea600e..38616f5 100644
--- a/src/gpm-graph-widget.c
+++ b/src/gpm-graph-widget.c
@@ -315,7 +315,7 @@ gpm_graph_widget_init (GpmGraphWidget *graph)
 	graph->priv->stop_y = 100;
 	graph->priv->use_grid = TRUE;
 	graph->priv->use_legend = FALSE;
-	graph->priv->data_list = g_ptr_array_new ();
+	graph->priv->data_list = g_ptr_array_new_with_free_func ((GDestroyNotify) g_ptr_array_unref);
 	graph->priv->plot_list = g_ptr_array_new ();
 	graph->priv->key_data = NULL;
 	graph->priv->type_x = GPM_GRAPH_WIDGET_TYPE_TIME;
@@ -338,17 +338,8 @@ gpm_graph_widget_init (GpmGraphWidget *graph)
 gboolean
 gpm_graph_widget_data_clear (GpmGraphWidget *graph)
 {
-	guint i;
-	GPtrArray *array;
-
 	g_return_val_if_fail (GPM_IS_GRAPH_WIDGET (graph), FALSE);
 
-	for (i=0; i<graph->priv->data_list->len; i++) {
-		array = g_ptr_array_index (graph->priv->data_list, i);
-		g_ptr_array_foreach (array, (GFunc) gpm_point_obj_free, NULL);
-		g_ptr_array_free (array, TRUE);
-	}
-
 	g_ptr_array_set_size (graph->priv->data_list, 0);
 	g_ptr_array_set_size (graph->priv->plot_list, 0);
 
@@ -370,8 +361,8 @@ gpm_graph_widget_finalize (GObject *object)
 	gpm_graph_widget_data_clear (graph);
 
 	/* free data */
-	g_ptr_array_free (graph->priv->data_list, TRUE);
-	g_ptr_array_free (graph->priv->plot_list, TRUE);
+	g_ptr_array_unref (graph->priv->data_list);
+	g_ptr_array_unref (graph->priv->plot_list);
 
 	context = pango_layout_get_context (graph->priv->layout);
 	g_object_unref (graph->priv->layout);
@@ -397,7 +388,7 @@ gpm_graph_widget_data_assign (GpmGraphWidget *graph, GpmGraphWidgetPlot plot, GP
 	g_return_val_if_fail (GPM_IS_GRAPH_WIDGET (graph), FALSE);
 
 	/* make a deep copy */
-	copy = g_ptr_array_new ();
+	copy = g_ptr_array_new_with_free_func ((GDestroyNotify) gpm_point_obj_free);
 	for (i=0; i<data->len; i++) {
 		obj = gpm_point_obj_copy (g_ptr_array_index (data, i));
 		g_ptr_array_add (copy, obj);
diff --git a/src/gpm-manager.c b/src/gpm-manager.c
index 0f69e57..e8151c9 100644
--- a/src/gpm-manager.c
+++ b/src/gpm-manager.c
@@ -1358,7 +1358,7 @@ gpm_manager_engine_just_laptop_battery (GpmManager *manager)
 {
 	DkpDevice *device;
 	DkpDeviceType type;
-	const GPtrArray *array;
+	GPtrArray *array;
 	gboolean ret = TRUE;
 	guint i;
 
@@ -1373,7 +1373,7 @@ gpm_manager_engine_just_laptop_battery (GpmManager *manager)
 			break;
 		}
 	}
-
+	g_ptr_array_unref (array);
 	return ret;
 }
 
diff --git a/src/gpm-prefs-core.c b/src/gpm-prefs-core.c
index 77b4e3a..fcd2d59 100644
--- a/src/gpm-prefs-core.c
+++ b/src/gpm-prefs-core.c
@@ -405,7 +405,7 @@ gpm_prefs_setup_action_combo (GpmPrefs *prefs, const gchar *widget_name,
 			 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
 	}
 
-	g_ptr_array_free (array, TRUE);
+	g_ptr_array_unref (array);
 	g_free (value_txt);
 }
 
diff --git a/src/gpm-statistics.c b/src/gpm-statistics.c
index 7aa912e..8b8db53 100644
--- a/src/gpm-statistics.c
+++ b/src/gpm-statistics.c
@@ -253,7 +253,7 @@ gpm_stats_update_smooth_data (GPtrArray *list)
 	convolved = egg_array_float_convolve (outliers, gaussian);
 
 	/* add the smoothed data back into a new array */
-	new = g_ptr_array_new ();
+	new = g_ptr_array_new_with_free_func ((GDestroyNotify) gpm_point_obj_free);
 	for (i=0; i<list->len; i++) {
 		point = (GpmPointObj *) g_ptr_array_index (list, i);
 		point_new = g_new0 (GpmPointObj, 1);
@@ -510,8 +510,7 @@ gpm_stats_set_graph_data (GtkWidget *widget, GPtrArray *data, gboolean use_smoot
 		if (use_points)
 			gpm_graph_widget_data_assign (GPM_GRAPH_WIDGET (widget), GPM_GRAPH_WIDGET_PLOT_POINTS, data);
 		gpm_graph_widget_data_assign (GPM_GRAPH_WIDGET (widget), GPM_GRAPH_WIDGET_PLOT_LINE, smoothed);
-		g_ptr_array_foreach (smoothed, (GFunc) gpm_point_obj_free, NULL);
-		g_ptr_array_free (smoothed, TRUE);
+		g_ptr_array_unref (smoothed);
 	}
 
 	/* show */
@@ -535,7 +534,7 @@ gpm_stats_update_info_page_history (DkpDevice *device)
 	gint32 offset = 0;
 	GTimeVal timeval;
 
-	new = g_ptr_array_new ();
+	new = g_ptr_array_new_with_free_func ((GDestroyNotify) gpm_point_obj_free);
 	if (g_strcmp0 (history_type, GPM_HISTORY_CHARGE_VALUE) == 0) {
 		g_object_set (graph_history,
 			      "type-x", GPM_GRAPH_WIDGET_TYPE_TIME,
@@ -620,10 +619,8 @@ gpm_stats_update_info_page_history (DkpDevice *device)
 	/* present data to graph */
 	gpm_stats_set_graph_data (graph_history, new, checked, points);
 
-	g_ptr_array_foreach (array, (GFunc) dkp_history_obj_free, NULL);
-	g_ptr_array_free (array, TRUE);
-	g_ptr_array_foreach (new, (GFunc) gpm_point_obj_free, NULL);
-	g_ptr_array_free (new, TRUE);
+	g_ptr_array_unref (array);
+	g_ptr_array_unref (new);
 out:
 	return;
 }
@@ -645,7 +642,7 @@ gpm_stats_update_info_page_stats (DkpDevice *device)
 	gboolean use_data = FALSE;
 	const gchar *type = NULL;
 
-	new = g_ptr_array_new ();
+	new = g_ptr_array_new_with_free_func ((GDestroyNotify) gpm_point_obj_free);
 	if (g_strcmp0 (stats_type, GPM_STATS_CHARGE_DATA_VALUE) == 0) {
 		type = "charging";
 		use_data = TRUE;
@@ -713,10 +710,8 @@ gpm_stats_update_info_page_stats (DkpDevice *device)
 	/* present data to graph */
 	gpm_stats_set_graph_data (graph_statistics, new, checked, points);
 
-	g_ptr_array_foreach (array, (GFunc) dkp_stats_obj_free, NULL);
-	g_ptr_array_free (array, TRUE);
-	g_ptr_array_foreach (new, (GFunc) gpm_point_obj_free, NULL);
-	g_ptr_array_free (new, TRUE);
+	g_ptr_array_unref (array);
+	g_ptr_array_unref (new);
 out:
 	return;
 }
@@ -1027,8 +1022,7 @@ gpm_stats_update_wakeups_data (void)
 		obj = g_ptr_array_index (array, i);
 		gpm_stats_add_wakeups_obj (obj);
 	}
-	g_ptr_array_foreach (array, (GFunc) dkp_wakeups_obj_free, NULL);
-	g_ptr_array_free (array, TRUE);
+	g_ptr_array_unref (array);
 }
 
 static void
@@ -1760,9 +1754,7 @@ main (int argc, char *argv[])
 			gtk_tree_path_free (path);
 		}
 	}
-
-	g_ptr_array_foreach (devices, (GFunc) g_object_unref, NULL);
-	g_ptr_array_free (devices, TRUE);
+	g_ptr_array_unref (devices);
 
 	/* set axis */
 	widget = GTK_WIDGET (gtk_builder_get_object (builder, "combobox_history_type"));
diff --git a/src/gpm-tray-icon.c b/src/gpm-tray-icon.c
index cf272b0..b90ecbe 100644
--- a/src/gpm-tray-icon.c
+++ b/src/gpm-tray-icon.c
@@ -541,7 +541,7 @@ gpm_tray_icon_activate_cb (GtkStatusIcon *status_icon, GpmTrayIcon *icon)
 	GtkWidget *item;
 	GtkWidget *image;
 	guint dev_cnt = 0;
-	const GPtrArray *array;
+	GPtrArray *array;
 	egg_debug ("icon left clicked");
 
 	/* add all device types to the drop down menu */
@@ -552,7 +552,7 @@ gpm_tray_icon_activate_cb (GtkStatusIcon *status_icon, GpmTrayIcon *icon)
 	dev_cnt += gpm_tray_icon_add_device (icon, menu, array, DKP_DEVICE_TYPE_KEYBOARD);
 	dev_cnt += gpm_tray_icon_add_device (icon, menu, array, DKP_DEVICE_TYPE_PDA);
 	dev_cnt += gpm_tray_icon_add_device (icon, menu, array, DKP_DEVICE_TYPE_PHONE);
-	g_ptr_array_foreach ((GPtrArray*)array, (GFunc) g_object_unref, NULL);
+	g_ptr_array_unref (array);
 
 	/* nothing to display! */
 	if (dev_cnt == 0 && !icon->priv->show_suspend && !icon->priv->show_hibernate)



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