diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2012-03-17 14:48:58 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2012-03-17 14:48:58 -0500 |
commit | 22a82f675f9552756ecac17b0a53a09bb20d1b46 (patch) | |
tree | 596cfb39fadb4cdd6bd8303b1ed1419e79451f12 /src/indicator-power.c | |
parent | a9ab1c3e64481d4ea4cd6d540b0dc75b055193d1 (diff) | |
download | ayatana-indicator-power-22a82f675f9552756ecac17b0a53a09bb20d1b46.tar.gz ayatana-indicator-power-22a82f675f9552756ecac17b0a53a09bb20d1b46.tar.bz2 ayatana-indicator-power-22a82f675f9552756ecac17b0a53a09bb20d1b46.zip |
Fix memory leaks in get_primary_device().
1. All the calls to g_variant_get_child_value() were leaked. Fixed by changing the use to g_variant_get_child() and keeping index values of the interesting children instead of pointers to them.
2. There were several paths where the local string "object_path" and "device_icon" were leaked. (For example, any non-battery entry given to us by upower). Fixed by making these const strings and peeking them from the variant with "&s" instead of "s".
Diffstat (limited to 'src/indicator-power.c')
-rw-r--r-- | src/indicator-power.c | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/src/indicator-power.c b/src/indicator-power.c index d97ecdd..701415d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -688,38 +688,34 @@ build_menu (IndicatorPower *self) static GVariant * get_primary_device (GVariant *devices) { - UpDeviceKind kind; - UpDeviceState state; - GVariant *device; - GVariant *primary_device_charging = NULL; - GVariant *primary_device_discharging = NULL; - GVariant *primary_device = NULL; + gint primary_device_charging_index = -1; + gint primary_device_discharging_index = -1; + gint primary_device_index = -1; gboolean charging = FALSE; gboolean discharging = FALSE; - gchar *object_path; - gchar *device_icon; - gdouble percentage; - guint64 time; guint64 min_discharging_time = G_MAXUINT64; guint64 max_charging_time = 0; - gsize n_devices; guint i; - n_devices = devices ? g_variant_n_children (devices) : 0; + const gsize n_devices = devices ? g_variant_n_children (devices) : 0; g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); for (i = 0; i < n_devices; i++) { - time = 0; - device = g_variant_get_child_value (devices, i); - g_variant_get (device, - "(susdut)", - &object_path, - &kind, - &device_icon, - &percentage, - &state, - &time); + const gchar *object_path; + UpDeviceKind kind; + const gchar *device_icon; + gdouble percentage; + UpDeviceState state; + guint64 time = 0; + + g_variant_get_child (devices, i, "(&su&sdut)", + &object_path, + &kind, + &device_icon, + &percentage, + &state, + &time); g_debug ("%s: got data from object %s", G_STRFUNC, object_path); @@ -738,7 +734,7 @@ get_primary_device (GVariant *devices) if (time < min_discharging_time) { min_discharging_time = time; - primary_device_discharging = device; + primary_device_discharging_index = i; } } else if (state == UP_DEVICE_STATE_CHARGING) @@ -746,33 +742,33 @@ get_primary_device (GVariant *devices) charging = TRUE; if (time == 0) /* Battery broken */ { - primary_device_charging = device; + primary_device_charging_index = i; } if (time > max_charging_time) { max_charging_time = time; - primary_device_charging = device; + primary_device_charging_index = i; } } else { - primary_device = device; + primary_device_index = i; } - - g_free (device_icon); - g_free (object_path); } if (discharging) { - primary_device = primary_device_discharging; + primary_device_index = primary_device_discharging_index; } else if (charging) { - primary_device = primary_device_charging; + primary_device_index = primary_device_charging_index; } - return primary_device; + if (primary_device_index >= 0) + return g_variant_get_child_value (devices, primary_device_index); + + return NULL; } static void |