diff options
author | Marco Trevisan (TreviƱo) <mail@3v1n0.net> | 2021-04-13 19:47:41 +0200 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2021-04-13 21:00:12 +0100 |
commit | ad29e6cf8dacff144804ab1fb04b096b0d04d4d7 (patch) | |
tree | 44815a4ef7b56a4d2d71d43ff0d96584bf521099 | |
parent | 81b9ea72f9982e7c25b09434492140d452acf215 (diff) | |
download | gusb-ad29e6cf8dacff144804ab1fb04b096b0d04d4d7.tar.gz |
device: Reference the libusb device when set
We used to reference a device during the initable init vfunc, however
it's better to do it when the device is actually set as an object
property.
In this way, the device is always unreffed when the object is disposed,
without having to care about the ordering.
-rw-r--r-- | gusb/gusb-device.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index 4aa1071..3f782f3 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -110,6 +110,18 @@ g_usb_device_get_property (GObject *object, } static void +set_libusb_device (GUsbDevice *device, + struct libusb_device *dev) +{ + GUsbDevicePrivate *priv = device->priv; + + g_clear_pointer (&priv->device, libusb_unref_device); + + if (dev != NULL) + priv->device = libusb_ref_device (dev); +} + +static void g_usb_device_set_property (GObject *object, guint prop_id, const GValue *value, @@ -120,7 +132,7 @@ g_usb_device_set_property (GObject *object, switch (prop_id) { case PROP_LIBUSB_DEVICE: - priv->device = g_value_get_pointer (value); + set_libusb_device (device, g_value_get_pointer (value)); break; case PROP_CONTEXT: priv->context = g_value_dup_object (value); @@ -239,8 +251,6 @@ g_usb_device_initable_init (GInitable *initable, return FALSE; } - libusb_ref_device (priv->device); - rc = libusb_get_device_descriptor (priv->device, &priv->desc); if (rc != LIBUSB_SUCCESS) { g_set_error (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_INTERNAL, |