Fix up some more memory leaks

This commit is contained in:
Richard Hughes 2009-09-11 12:34:25 +01:00
parent ca604fe92b
commit 8cb468ce64
4 changed files with 52 additions and 30 deletions

View file

@ -189,7 +189,7 @@ dkp_daemon_get_on_battery_local (DkpDaemon *daemon)
gboolean result = FALSE;
gboolean on_battery;
DkpDevice *device;
const GPtrArray *array;
GPtrArray *array;
/* ask each device */
array = dkp_device_list_get_array (daemon->priv->power_devices);
@ -201,6 +201,7 @@ dkp_daemon_get_on_battery_local (DkpDaemon *daemon)
break;
}
}
g_ptr_array_unref (array);
return result;
}
@ -212,7 +213,7 @@ dkp_daemon_get_number_devices_of_type (DkpDaemon *daemon, DkpDeviceType type)
{
guint i;
DkpDevice *device;
const GPtrArray *array;
GPtrArray *array;
DkpDeviceType type_tmp;
guint count = 0;
@ -226,6 +227,7 @@ dkp_daemon_get_number_devices_of_type (DkpDaemon *daemon, DkpDeviceType type)
if (type == type_tmp)
count++;
}
g_ptr_array_unref (array);
return count;
}
@ -242,7 +244,7 @@ dkp_daemon_get_on_low_battery_local (DkpDaemon *daemon)
gboolean result = TRUE;
gboolean on_low_battery;
DkpDevice *device;
const GPtrArray *array;
GPtrArray *array;
/* ask each device */
array = dkp_device_list_get_array (daemon->priv->power_devices);
@ -254,6 +256,7 @@ dkp_daemon_get_on_low_battery_local (DkpDaemon *daemon)
break;
}
}
g_ptr_array_unref (array);
return result;
}
@ -270,7 +273,7 @@ dkp_daemon_get_on_ac_local (DkpDaemon *daemon)
gboolean result = FALSE;
gboolean online;
DkpDevice *device;
const GPtrArray *array;
GPtrArray *array;
/* ask each device */
array = dkp_device_list_get_array (daemon->priv->power_devices);
@ -282,6 +285,7 @@ dkp_daemon_get_on_ac_local (DkpDaemon *daemon)
break;
}
}
g_ptr_array_unref (array);
return result;
}
@ -318,7 +322,7 @@ static gboolean
dkp_daemon_refresh_battery_devices (DkpDaemon *daemon)
{
guint i;
const GPtrArray *array;
GPtrArray *array;
DkpDevice *device;
DkpDeviceType type;
@ -333,6 +337,7 @@ dkp_daemon_refresh_battery_devices (DkpDaemon *daemon)
if (type == DKP_DEVICE_TYPE_BATTERY)
dkp_device_refresh_internal (device);
}
g_ptr_array_unref (array);
return TRUE;
}
@ -344,7 +349,7 @@ gboolean
dkp_daemon_enumerate_devices (DkpDaemon *daemon, DBusGMethodInvocation *context)
{
guint i;
const GPtrArray *array;
GPtrArray *array;
GPtrArray *object_paths;
DkpDevice *device;
@ -355,6 +360,7 @@ dkp_daemon_enumerate_devices (DkpDaemon *daemon, DBusGMethodInvocation *context)
device = (DkpDevice *) g_ptr_array_index (array, i);
g_ptr_array_add (object_paths, g_strdup (dkp_device_get_object_path (device)));
}
g_ptr_array_unref (array);
/* return it on the bus */
dbus_g_method_return (context, object_paths);
@ -587,8 +593,8 @@ dkp_daemon_device_added_cb (DkpBackend *backend, GObject *native, DkpDevice *dev
const gchar *object_path;
g_return_if_fail (DKP_IS_DAEMON (daemon));
g_return_if_fail (native != NULL);
g_return_if_fail (device != NULL);
g_return_if_fail (DKP_IS_DEVICE (device));
g_return_if_fail (G_IS_OBJECT (native));
/* add to device list */
dkp_device_list_insert (daemon->priv->power_devices, native, G_OBJECT (device));
@ -612,8 +618,8 @@ dkp_daemon_device_changed_cb (DkpBackend *backend, GObject *native, DkpDevice *d
gboolean ret;
g_return_if_fail (DKP_IS_DAEMON (daemon));
g_return_if_fail (native != NULL);
g_return_if_fail (device != NULL);
g_return_if_fail (DKP_IS_DEVICE (device));
g_return_if_fail (G_IS_OBJECT (native));
/* refresh battery devices when AC state changes */
g_object_get (device,
@ -656,8 +662,8 @@ dkp_daemon_device_removed_cb (DkpBackend *backend, GObject *native, DkpDevice *d
const gchar *object_path;
g_return_if_fail (DKP_IS_DAEMON (daemon));
g_return_if_fail (native != NULL);
g_return_if_fail (device != NULL);
g_return_if_fail (DKP_IS_DEVICE (device));
g_return_if_fail (G_IS_OBJECT (native));
/* remove from list */
dkp_device_list_remove (daemon->priv->power_devices, G_OBJECT(device));
@ -668,6 +674,9 @@ dkp_daemon_device_removed_cb (DkpBackend *backend, GObject *native, DkpDevice *d
egg_debug ("emitting device-removed: %s", object_path);
g_signal_emit (daemon, signals[SIGNAL_DEVICE_REMOVED], 0, object_path);
}
/* finalise the object */
g_object_unref (device);
}
/**
@ -676,6 +685,8 @@ dkp_daemon_device_removed_cb (DkpBackend *backend, GObject *native, DkpDevice *d
static void
dkp_daemon_properties_changed_cb (GObject *object, GParamSpec *pspec, DkpDaemon *daemon)
{
g_return_if_fail (DKP_IS_DAEMON (daemon));
/* emit */
if (!daemon->priv->during_coldplug) {
egg_debug ("emitting changed");

View file

@ -47,6 +47,8 @@ G_DEFINE_TYPE (DkpDeviceList, dkp_device_list, G_TYPE_OBJECT)
*
* Convert a native %GObject into a %DkpDevice -- we use the native path
* to look these up as it's the only thing they share.
*
* Return value: the object, or %NULL if not found. Free with g_object_unref()
**/
GObject *
dkp_device_list_lookup (DkpDeviceList *list, GObject *native)
@ -58,8 +60,12 @@ dkp_device_list_lookup (DkpDeviceList *list, GObject *native)
/* does device exist in db? */
native_path = dkp_native_get_native_path (native);
if (native_path == NULL)
return NULL;
device = g_hash_table_lookup (list->priv->map_native_path_to_device, native_path);
return device;
if (device == NULL)
return NULL;
return g_object_ref (device);
}
/**
@ -79,7 +85,7 @@ dkp_device_list_insert (DkpDeviceList *list, GObject *native, GObject *device)
native_path = dkp_native_get_native_path (native);
g_hash_table_insert (list->priv->map_native_path_to_device,
g_strdup (native_path), device);
g_strdup (native_path), g_object_ref (device));
g_ptr_array_add (list->priv->array, g_object_ref (device));
egg_debug ("added %s", native_path);
return TRUE;
@ -111,7 +117,6 @@ dkp_device_list_remove (DkpDeviceList *list, GObject *device)
g_hash_table_foreach_remove (list->priv->map_native_path_to_device,
dkp_device_list_remove_cb, device);
g_ptr_array_remove (list->priv->array, device);
g_object_unref (device);
return TRUE;
}
@ -119,12 +124,14 @@ dkp_device_list_remove (DkpDeviceList *list, GObject *device)
* dkp_device_list_get_array:
*
* This is quick to iterate when we don't have GObject's to resolve
*
* Return value: the array, free with g_ptr_array_unref()
**/
const GPtrArray *
GPtrArray *
dkp_device_list_get_array (DkpDeviceList *list)
{
g_return_val_if_fail (DKP_IS_DEVICE_LIST (list), NULL);
return list->priv->array;
return g_ptr_array_ref (list->priv->array);
}
/**
@ -147,8 +154,8 @@ static void
dkp_device_list_init (DkpDeviceList *list)
{
list->priv = DKP_DEVICE_LIST_GET_PRIVATE (list);
list->priv->array = g_ptr_array_new ();
list->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
list->priv->array = g_ptr_array_new_with_free_func (g_object_unref);
list->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
}
/**
@ -164,7 +171,6 @@ dkp_device_list_finalize (GObject *object)
list = DKP_DEVICE_LIST (object);
g_ptr_array_foreach (list->priv->array, (GFunc) g_object_unref, NULL);
g_ptr_array_free (list->priv->array, TRUE);
g_hash_table_unref (list->priv->map_native_path_to_device);

View file

@ -58,7 +58,7 @@ gboolean dkp_device_list_insert (DkpDeviceList *list,
GObject *device);
gboolean dkp_device_list_remove (DkpDeviceList *list,
GObject *device);
const GPtrArray *dkp_device_list_get_array (DkpDeviceList *list);
GPtrArray *dkp_device_list_get_array (DkpDeviceList *list);
G_END_DECLS

View file

@ -184,7 +184,8 @@ dkp_backend_device_changed (DkpBackend *backend, GUdevDevice *native)
egg_debug ("emitting changed %s", dkp_device_get_object_path (device));
g_signal_emit (backend, signals[SIGNAL_DEVICE_CHANGED], 0, native, device);
out:
return;
if (object != NULL)
g_object_unref (object);
}
/**
@ -218,6 +219,8 @@ dkp_backend_device_add (DkpBackend *backend, GUdevDevice *native)
g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, native, device);
g_object_unref (device);
out:
if (object != NULL)
g_object_unref (object);
return ret;
}
@ -234,15 +237,17 @@ dkp_backend_device_remove (DkpBackend *backend, GUdevDevice *native)
object = dkp_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
if (object == NULL) {
egg_warning ("ignoring remove event on %s", g_udev_device_get_sysfs_path (native));
} else {
device = DKP_DEVICE (object);
/* emit */
egg_debug ("emitting device-removed: %s", g_udev_device_get_sysfs_path (native));
g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, native, device);
/* destroy */
g_object_unref (device);
goto out;
}
device = DKP_DEVICE (object);
/* emit */
egg_debug ("emitting device-removed: %s", g_udev_device_get_sysfs_path (native));
g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, native, device);
out:
if (object != NULL)
g_object_unref (object);
}
/**