From caf7eb33e4861e6d61a4490cc820858d8ba6901d Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Thu, 9 Nov 2023 09:23:07 -0500 Subject: [PATCH] alsa: Fix ctl device cleanup order Reorder removing sources, closing the ctl device, and freeing memory, so that there's no chance of races causing a crash. --- spa/plugins/alsa/alsa-pcm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index dd805b621..b84ffbd1c 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -838,6 +838,13 @@ int spa_alsa_clear(struct state *state) free(state->tag[1]); if (state->ctl) { + for (int i = 0; i < state->ctl_n_fds; i++) { + spa_loop_remove_source(state->data_loop, &state->ctl_sources[i]); + } + + snd_ctl_close(state->ctl); + state->ctl = NULL; + for (unsigned int i = 0; i < state->num_bind_ctls; i++) { if (state->bound_ctls[i].info) { snd_ctl_elem_info_free(state->bound_ctls[i].info); @@ -848,13 +855,6 @@ int spa_alsa_clear(struct state *state) state->bound_ctls[i].value = NULL; } } - - for (int i = 0; i < state->ctl_n_fds; i++) { - spa_loop_remove_source(state->data_loop, &state->ctl_sources[i]); - } - - snd_ctl_close(state->ctl); - state->ctl = NULL; } return err;