Diff between 8332abd8fe8cfb88aa323b315eb228f0d0f581ff and f1e7f81efecda34338e3b4245ac419db5a236192

Changed Files

File Additions Deletions Status
src/device.c +10 -9 modified

Full Patch

diff --git a/src/device.c b/src/device.c
index e1219c4..a760f78 100644
--- a/src/device.c
+++ b/src/device.c
@@ -139,6 +139,8 @@ struct att_callbacks {
 };
 
 struct btd_device {
+	int ref_count;
+
 	bdaddr_t	bdaddr;
 	uint8_t		bdaddr_type;
 	char		*path;
@@ -188,8 +190,6 @@ struct btd_device {
 	bool		legacy;
 	int8_t		rssi;
 
-	gint		ref;
-
 	GIOChannel	*att_io;
 	guint		cleanup_id;
 	guint		store_id;
@@ -3981,21 +3981,22 @@ const sdp_record_t *btd_device_get_record(struct btd_device *device,
 
 struct btd_device *btd_device_ref(struct btd_device *device)
 {
-	device->ref++;
-
-	DBG("%p: ref=%d", device, device->ref);
+	__sync_fetch_and_add(&device->ref_count, 1);
 
 	return device;
 }
 
 void btd_device_unref(struct btd_device *device)
 {
-	device->ref--;
-
-	DBG("%p: ref=%d", device, device->ref);
+	if (__sync_sub_and_fetch(&device->ref_count, 1))
+		return;
 
-	if (device->ref > 0)
+	if (!device->path) {
+		error("freeing device without an object path");
 		return;
+	}
+
+	DBG("Freeing device %s", device->path);
 
 	g_dbus_unregister_interface(dbus_conn, device->path, DEVICE_INTERFACE);
 }