From e66bc06cfda0e183f77f40f39fb03e6782023511 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Fri, 26 Jun 2009 15:02:56 +0100 Subject: [PATCH] Fix a small memory leak on supply coldplugging --- src/dkp-device-supply.c | 45 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/dkp-device-supply.c b/src/dkp-device-supply.c index b11d82c..8f6e5cb 100644 --- a/src/dkp-device-supply.c +++ b/src/dkp-device-supply.c @@ -274,6 +274,31 @@ dkp_device_supply_convert_device_technology (const gchar *type) return DKP_DEVICE_TECHNOLOGY_UNKNOWN; } +/** + * dkp_device_supply_get_string: + **/ +static gchar * +dkp_device_supply_get_string (const gchar *native_path, const gchar *key) +{ + gchar *value; + + /* get value, and strip to remove spaces */ + value = g_strstrip (sysfs_get_string (native_path, key)); + + /* no value */ + if (value == NULL) + goto out; + + /* empty value */ + if (value[0] == '\0') { + g_free (value); + value = NULL; + goto out; + } +out: + return value; +} + /** * dkp_device_supply_refresh_battery: * @@ -301,6 +326,9 @@ dkp_device_supply_refresh_battery (DkpDeviceSupply *supply) gdouble voltage; guint64 time_to_empty; guint64 time_to_full; + gchar *manufacturer; + gchar *model_name; + gchar *serial_number; d = dkp_device_get_d (device); if (d == NULL) { @@ -341,19 +369,28 @@ dkp_device_supply_refresh_battery (DkpDeviceSupply *supply) g_object_set (device, "power-supply", TRUE, NULL); /* the ACPI spec is bad at defining battery type constants */ - technology_native = g_strstrip (sysfs_get_string (native_path, "technology")); + technology_native = dkp_device_supply_get_string (native_path, "technology"); g_object_set (device, "technology", dkp_device_supply_convert_device_technology (technology_native), NULL); g_free (technology_native); + /* get values which may be blank */ + manufacturer = dkp_device_supply_get_string (native_path, "manufacturer"); + model_name = dkp_device_supply_get_string (native_path, "model_name"); + serial_number = dkp_device_supply_get_string (native_path, "serial_number"); + g_object_set (device, - "vendor", g_strstrip (sysfs_get_string (native_path, "manufacturer")), - "model", g_strstrip (sysfs_get_string (native_path, "model_name")), - "serial", g_strstrip (sysfs_get_string (native_path, "serial_number")), + "vendor", manufacturer, + "model", model_name, + "serial", serial_number, "is-rechargeable", TRUE, /* assume true for laptops */ "has-history", TRUE, "has-statistics", TRUE, NULL); + g_free (manufacturer); + g_free (model_name); + g_free (serial_number); + /* these don't change at runtime */ energy_full = sysfs_get_double (native_path, "energy_full") / 1000000.0; energy_full_design = sysfs_get_double (native_path, "energy_full_design") / 1000000.0;