+static
+void bt_connection_try_remove_from_graph(struct bt_connection *connection)
+{
+ void *graph = bt_object_borrow_parent(&connection->base);
+
+ if (connection->base.ref_count.count > 0 ||
+ connection->downstream_port ||
+ connection->upstream_port ||
+ connection->iterators->len > 0) {
+ return;
+ }
+
+ /*
+ * At this point we know that:
+ *
+ * 1. The connection is dead (ports were disconnected).
+ * 2. All the notification iterators that this connection
+ * created, if any, are finalized.
+ * 3. The connection's reference count is 0, so only the
+ * parent (graph) owns this connection after this call.
+ *
+ * In other words, no other object than the graph knows this
+ * connection.
+ *
+ * It is safe to remove the connection from the graph, therefore
+ * destroying it.
+ */
+ bt_graph_remove_connection(graph, connection);
+}
+
+static
+void bt_connection_parent_is_owner(struct bt_object *obj)
+{
+ struct bt_connection *connection = container_of(obj,
+ struct bt_connection, base);
+
+ bt_connection_try_remove_from_graph(connection);
+}
+