alsa-project/alsa-lib issue #419 was edited from wangtao13: I wrote an ALSA application using alsa-lib-1.2.9, the main logic of the application is ``` static int init_pcm_play(snd_pcm_t **playback_handle...) { snd_pcm_hw_params_t *hw_params; snd_pcm_format_t format; snd_pcm_uframes_t frames, periodsize; int ret; //1. openPCM, if (0 > snd_pcm_open(playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) { return -1; } //2. snd_pcm_hw_params_t if (0 > snd_pcm_hw_params_malloc (&hw_params)) { return -1; } //3. hw_params if (0 > snd_pcm_hw_params_any (*playback_handle, hw_params)) { return -1; } //4. if (0 > snd_pcm_hw_params_set_access (*playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) { return -1; } //5. SND_PCM_FORMAT_U8,8 if (8 == bits_per_sample) { format = SND_PCM_FORMAT_U8; } if (16 == bits_per_sample) { format = SND_PCM_FORMAT_S16_LE; } if (0 > snd_pcm_hw_params_set_format (*playback_handle, hw_params, format)) { return -1; } //6. if (0 > snd_pcm_hw_params_set_rate_near (*playback_handle, hw_params, &rate, 0)) { return -1; } //7. if (0 > snd_pcm_hw_params_set_channels(*playback_handle, hw_params, channels)) { return -1; } frames = 64; periodsize = frames; ret = snd_pcm_hw_params_set_buffer_size_near(*playback_handle, hw_params, &periodsize); if (ret < 0) { printf("Unable to set buffer size %li : %s\n", frames * 2, snd_strerror(ret)); } periodsize /= channels; ret = snd_pcm_hw_params_set_period_size_near(*playback_handle, hw_params, &periodsize, 0); if (ret < 0) { printf("Unable to set period size %li : %s\n", periodsize, snd_strerror(ret)); } //8. set hw_params if (0 > snd_pcm_hw_params (*playback_handle, hw_params)) { return -1; } snd_pcm_uframes_t temp; snd_pcm_hw_params_get_buffer_size(hw_params, &temp); printf("Got buffer size: %ld\n", temp); snd_pcm_hw_params_get_period_size(hw_params, &temp, 0); printf("Got period size: %ld\n", temp); snd_pcm_hw_params_free(hw_params); ret = snd_pcm_prepare(playback_handle); if (ret < 0) { printf("snd_pcm_prepare err\n"); } return ret; } int sound_play(playback_handle, ...) { rc = snd_pcm_writei(playback_handle, buf, buflen); if (rc < 0) { if (rc == -EAGAIN) { usleep(1000); return -1; } snd_pcm_prepare(playback_handle); return -1; } else if (rc == 0) { usleep(10 * 1000); } return 0; } int app_exit(....) { snd_pcm_close(*playback_handle); snd_config_update_free_global(); } #define TEST_LOOPS 2 /* > 1, can trigger memory leakage */ int main_test() { init_pcm_play(&playback_handle, ...., ..); for (int i=0; i<2; i++) { pcm_exit(); init_pcm_play(&playback_handle); sound_play(playback_handle) } return 0; } ``` ``` ==5176==ERROR: LeakSanitizer: detected memory leaks Direct leak of 12792 byte(s) in 39 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x7626c458 (/usr/lib/libasound.so.2+0x4c458) Direct leak of 12 byte(s) in 1 object(s) allocated from: #0 0x77901ad8 in __interceptor_posix_memalign /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:155 #1 0x7758b3f4 in av_malloc (/usr/lib/libavutil.so.56+0x3b3f4) Indirect leak of 151552 byte(s) in 37 object(s) allocated from: #0 0x77900acc in __interceptor_malloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:86 #1 0x76281230 in snd_pcm_mmap (/usr/lib/libasound.so.2+0x61230) Indirect leak of 37392 byte(s) in 114 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x7626c458 (/usr/lib/libasound.so.2+0x4c458) Indirect leak of 31200 byte(s) in 39 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762b91b8 in snd_pcm_softvol_open (/usr/lib/libasound.so.2+0x991b8) Indirect leak of 15808 byte(s) in 76 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x76280d1c in snd_pcm_mmap (/usr/lib/libasound.so.2+0x60d1c) Indirect leak of 10032 byte(s) in 38 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762b4e28 (/usr/lib/libasound.so.2+0x94e28) Indirect leak of 5168 byte(s) in 38 object(s) allocated from: #0 0x77900acc in __interceptor_malloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:86 #1 0x7627e644 (/usr/lib/libasound.so.2+0x5e644) Indirect leak of 3344 byte(s) in 38 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x7627e4e0 (/usr/lib/libasound.so.2+0x5e4e0) Indirect leak of 1976 byte(s) in 38 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762939ac in snd_pcm_plug_open (/usr/lib/libasound.so.2+0x739ac) Indirect leak of 1824 byte(s) in 76 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x76280d3c in snd_pcm_mmap (/usr/lib/libasound.so.2+0x60d3c) Indirect leak of 1596 byte(s) in 38 object(s) allocated from: #0 0x7786d584 in __interceptor_strdup /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_interceptors.cc:405 #1 0x762c09b8 in snd_timer_hw_open (/usr/lib/libasound.so.2+0xa09b8) Indirect leak of 1560 byte(s) in 39 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762b7c58 (/usr/lib/libasound.so.2+0x97c58) Indirect leak of 1560 byte(s) in 39 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762512d4 (/usr/lib/libasound.so.2+0x312d4) Indirect leak of 1520 byte(s) in 38 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762c0978 in snd_timer_hw_open (/usr/lib/libasound.so.2+0xa0978) Indirect leak of 1419 byte(s) in 153 object(s) allocated from: #0 0x7786d584 in __interceptor_strdup /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_interceptors.cc:405 #1 0x7626c47c (/usr/lib/libasound.so.2+0x4c47c) Indirect leak of 468 byte(s) in 39 object(s) allocated from: #0 0x77900d24 in __interceptor_calloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:95 #1 0x762572e8 in snd_ctl_hw_open (/usr/lib/libasound.so.2+0x372e8) Indirect leak of 304 byte(s) in 76 object(s) allocated from: #0 0x779011a4 in __interceptor_realloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:105 #1 0x762690c4 (/usr/lib/libasound.so.2+0x490c4) Indirect leak of 273 byte(s) in 1 object(s) allocated from: #0 0x779011a4 in __interceptor_realloc /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:105 #1 0x77569f3c in av_buffer_realloc (/usr/lib/libavutil.so.56+0x19f3c) Indirect leak of 195 byte(s) in 39 object(s) allocated from: #0 0x7786d584 in __interceptor_strdup /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_interceptors.cc:405 #1 0x762512fc (/usr/lib/libasound.so.2+0x312fc) Indirect leak of 28 byte(s) in 1 object(s) allocated from: #0 0x77901ad8 in __interceptor_posix_memalign /home/users/at/work/arm-gnu-toolchain/arm-gcc/libsanitizer/asan/asan_malloc_linux.cc:155 #1 0x7758b3f4 in av_malloc (/usr/lib/libavutil.so.56+0x3b3f4) SUMMARY: AddressSanitizer: 280023 byte(s) leaked in 997 allocation(s). ``` If the `TEST_LOOPS` is defined as 1, there is NO memory leakage reported by ASAN. Issue URL : https://github.com/alsa-project/alsa-lib/issues/419 Repository URL: https://github.com/alsa-project/alsa-lib