diff --git a/src/adapter.c b/src/adapter.c
index 3dc788b..1d0c0ec 100644
--- a/src/adapter.c
+++ b/src/adapter.c
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);
{
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
} agent_request_type_t;
struct agent {
+ int ref;
struct btd_adapter *adapter;
char *name;
char *path;
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)
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
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
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)))
DBG("Removing device %s", device->path);
if (device->agent)
- agent_free(device->agent);
+ agent_unref(device->agent);
if (device->bonding) {
uint8_t status;
return;
agent_cancel(device->agent);
- agent_free(device->agent);
+ agent_unref(device->agent);
device->agent = NULL;
}