From b04b52ecf87041f211cedcb950bfc0e3f29bf14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Mon, 20 Dec 2021 19:29:05 +0100 Subject: [PATCH] spa: alsa: do not look up the card again when releasing Currently, `release_card()` uses `find_card()` to find the card by the index stored in the state object. However, `find_card()` increments the reference count of the object, therefore `release_card()` will drop the reference that it has just created by calling `find_card()` and not the "calling scope's" reference. This prevents the card objects from being freed when the SPA handle is cleared. Fix it by having `release_card()` take a pointer to the card and not its index. --- spa/plugins/alsa/alsa-pcm.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index d2a2c8ee2..aa34be886 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -72,11 +72,9 @@ error: return NULL; } -static void release_card(uint32_t index) +static void release_card(struct card *c) { - struct card *c; - if ((c = find_card(index)) == NULL) - return; + spa_assert(c->ref > 0); if (--c->ref > 0) return; @@ -420,8 +418,11 @@ int spa_alsa_init(struct state *state, const struct spa_dict *info) int spa_alsa_clear(struct state *state) { - release_card(state->card_index); + release_card(state->card); + state->card = NULL; + state->card_index = SPA_ID_INVALID; + return 0; }