fp-context, tools: Use auto-ptr to handle GTypeClass ownership

This also fixes a small leak we might have if reffing a type that was not a
virtual one.
This commit is contained in:
Marco Trevisan (Treviño) 2019-12-13 20:40:41 +01:00
parent 43a8c909bf
commit eeddd8c7bc
3 changed files with 11 additions and 24 deletions

View file

@ -131,14 +131,12 @@ usb_device_added_cb (FpContext *self, GUsbDevice *device, GUsbContext *usb_ctx)
for (i = 0; i < priv->drivers->len; i++)
{
GType driver = g_array_index (priv->drivers, GType, i);
FpDeviceClass *cls = FP_DEVICE_CLASS (g_type_class_ref (driver));
g_autoptr(GTypeClass) type_class = g_type_class_ref (driver);
FpDeviceClass *cls = FP_DEVICE_CLASS (type_class);
const FpIdEntry *entry;
if (cls->type != FP_DEVICE_TYPE_USB)
{
g_type_class_unref (cls);
continue;
}
for (entry = cls->id_table; entry->pid; entry++)
{
@ -158,8 +156,6 @@ usb_device_added_cb (FpContext *self, GUsbDevice *device, GUsbContext *usb_ctx)
found_driver = driver;
found_entry = entry;
}
g_type_class_unref (cls);
}
if (found_driver == G_TYPE_NONE)
@ -355,7 +351,8 @@ fp_context_enumerate (FpContext *context)
for (i = 0; i < priv->drivers->len; i++)
{
GType driver = g_array_index (priv->drivers, GType, i);
FpDeviceClass *cls = FP_DEVICE_CLASS (g_type_class_ref (driver));
g_autoptr(GTypeClass) type_class = g_type_class_ref (driver);
FpDeviceClass *cls = FP_DEVICE_CLASS (type_class);
const FpIdEntry *entry;
if (cls->type != FP_DEVICE_TYPE_VIRTUAL)
@ -381,8 +378,6 @@ fp_context_enumerate (FpContext *context)
NULL);
g_debug ("created");
}
g_type_class_unref (cls);
}
while (priv->pending_devices)

View file

@ -38,14 +38,12 @@ insert_drivers (GList *list)
for (i = 0; i < drivers->len; i++)
{
GType driver = g_array_index (drivers, GType, i);
FpDeviceClass *cls = FP_DEVICE_CLASS (g_type_class_ref (driver));
g_autoptr(GTypeClass) type_class = g_type_class_ref (driver);
FpDeviceClass *cls = FP_DEVICE_CLASS (type_class);
const FpIdEntry *entry;
if (cls->type != FP_DEVICE_TYPE_USB)
{
g_type_class_unref (cls);
continue;
}
for (entry = cls->id_table; entry->vid; entry++)
{
@ -63,8 +61,6 @@ insert_drivers (GList *list)
list = g_list_prepend (list, g_strdup_printf ("%s | %s\n", key, cls->full_name));
}
g_type_class_unref (cls);
}
return list;

View file

@ -104,17 +104,13 @@ main (int argc, char **argv)
for (i = 0; i < drivers->len; i++)
{
GType driver = g_array_index (drivers, GType, i);
FpDeviceClass *cls = FP_DEVICE_CLASS (g_type_class_ref (driver));
g_autoptr(GTypeClass) type_class = g_type_class_ref (driver);
FpDeviceClass *cls = FP_DEVICE_CLASS (type_class);
if (cls->type != FP_DEVICE_TYPE_USB)
{
g_type_class_unref (cls);
continue;
}
print_driver (cls);
g_type_class_unref (cls);
}
print_driver (&whitelist);