Diff between 747c74538fcd1130ab038d944c0e4861c93ab42e and f989bfd87514d7ff577ccd32dfcbcb72719048ea

Changed Files

File Additions Deletions Status
src/adapter.c +2 -2 modified
src/agent.c +18 -4 modified
src/agent.h +2 -1 modified
src/device.c +3 -3 modified

Full Patch

diff --git a/src/adapter.c b/src/adapter.c
index 3dc788b..1d0c0ec 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1435,7 +1435,7 @@ static DBusMessage *unregister_agent(DBusConnection *conn, DBusMessage *msg,
 	if (!adapter->agent || !agent_matches(adapter->agent, name, path))
 		return btd_error_does_not_exist(msg);
 
-	agent_free(adapter->agent);
+	agent_unref(adapter->agent);
 	adapter->agent = NULL;
 
 	return dbus_message_new_method_return(msg);
@@ -2067,7 +2067,7 @@ static void adapter_free(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 
-	agent_free(adapter->agent);
+	agent_unref(adapter->agent);
 	adapter->agent = NULL;
 
 	DBG("%p", adapter);
diff --git a/src/agent.c b/src/agent.c
index c2033a6..5085cea 100644
--- a/src/agent.c
+++ b/src/agent.c
@@ -69,6 +69,7 @@ typedef enum {
 } agent_request_type_t;
 
 struct agent {
+	int ref;
 	struct btd_adapter *adapter;
 	char *name;
 	char *path;
@@ -149,12 +150,25 @@ static void agent_exited(DBusConnection *conn, void *user_data)
 
 	agent->exited = TRUE;
 
-	agent_free(agent);
+	agent_unref(agent);
 }
 
-void agent_free(struct agent *agent)
+struct agent *agent_ref(struct agent *agent)
 {
-	if (!agent)
+	agent->ref++;
+
+	DBG("%p: ref=%d", agent, agent->ref);
+
+	return agent;
+}
+
+void agent_unref(struct agent *agent)
+{
+	agent->ref--;
+
+	DBG("%p: ref=%d", agent, agent->ref);
+
+	if (agent->ref > 0)
 		return;
 
 	if (agent->remove_cb)
@@ -219,7 +233,7 @@ struct agent *agent_create(struct btd_adapter *adapter, const char *name,
 		g_dbus_add_disconnect_watch(btd_get_dbus_connection(), name,
 						agent_exited, agent, NULL);
 
-	return agent;
+	return agent_ref(agent);
 }
 
 static struct agent_request *agent_request_new(struct agent *agent,
diff --git a/src/agent.h b/src/agent.h
index f27459f..769cd82 100644
--- a/src/agent.h
+++ b/src/agent.h
@@ -39,7 +39,8 @@ struct agent *agent_create(struct btd_adapter *adapter, const char *name,
 				const char *path, uint8_t capability,
 				agent_remove_cb cb, void *remove_cb_data);
 
-void agent_free(struct agent *agent);
+struct agent *agent_ref(struct agent *agent);
+void agent_unref(struct agent *agent);
 
 int agent_authorize_service(struct agent *agent, const char *path,
 				const char *uuid, agent_cb cb,
diff --git a/src/device.c b/src/device.c
index c49cb81..f655800 100644
--- a/src/device.c
+++ b/src/device.c
@@ -388,7 +388,7 @@ static void device_free(gpointer user_data)
 	struct agent *agent = adapter_get_agent(adapter);
 
 	if (device->agent)
-		agent_free(device->agent);
+		agent_unref(device->agent);
 
 	if (agent && (agent_is_busy(agent, device) ||
 				agent_is_busy(agent, device->authr)))
@@ -2204,7 +2204,7 @@ void device_remove(struct btd_device *device, gboolean remove_stored)
 	DBG("Removing device %s", device->path);
 
 	if (device->agent)
-		agent_free(device->agent);
+		agent_unref(device->agent);
 
 	if (device->bonding) {
 		uint8_t status;
@@ -3126,7 +3126,7 @@ static void bonding_request_free(struct bonding_req *bonding)
 		return;
 
 	agent_cancel(device->agent);
-	agent_free(device->agent);
+	agent_unref(device->agent);
 	device->agent = NULL;
 }