soloud icon indicating copy to clipboard operation
soloud copied to clipboard

Valgrind reports a lot of memory leaks on ALSA

Open nurpax opened this issue 5 years ago • 3 comments

Expected behavior:

No leaks reported by Valgrind.

Actual behavior:

About a hundred or so of this or similar:

==21405==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21405==    by 0x5E5B5E8: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5B6AC: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5C204: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5E287: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5DF64: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5E44D: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E5E61B: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E6181F: snd_config_update_r (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E61DE9: snd_config_update_ref (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x5E7E6F9: snd_pcm_open (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==21405==    by 0x4E36E6: SoLoud::alsa_init(SoLoud::Soloud*, unsigned int, unsigned int, unsigned int, unsigned int) (soloud_alsa.cpp:120)

Steps to reproduce the problem:

Compile and run this program with -DWITH_ALSA on Linux Ubuntu 18.04:

#include "soloud_c.h"
static void soloud_repro() {
    Soloud* soloud = Soloud_create();
    Soloud_init(soloud);
    Soloud_deinit(soloud);
    Soloud_destroy(soloud);
}
int main() {
    soloud_repro();
}

Run under valgrind with:

valgrind --leak-check=full ./repro

SoLoud version, operating system, backend used, any other potentially useful information:

  • soloud20200207
  • Ubuntu 18.04
  • -DWITH_ALSA

nurpax avatar Jun 16 '20 20:06 nurpax

This seems to fix the leaks:

diff --git a/vendor/soloud20200207/src/backend/alsa/soloud_alsa.cpp b/vendor/soloud20200207/src/backend/alsa/soloud_alsa.cpp
index 3cd47a8..e85c77f 100644
--- a/vendor/soloud20200207/src/backend/alsa/soloud_alsa.cpp
+++ b/vendor/soloud20200207/src/backend/alsa/soloud_alsa.cpp
@@ -93,6 +93,7 @@ namespace SoLoud
         }
         snd_pcm_drain(data->alsaDeviceHandle);
         snd_pcm_close(data->alsaDeviceHandle);
+        snd_config_update_free_global();
         if (0 != data->sampleBuffer)
         {
             delete[] data->sampleBuffer;

I just have no idea if this is the right thing to do.

With this change, I get a completely clean valgrind report for the repro program (and also in a larger game app that I'm developing):

==21631== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21631== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==21631== Command: ./out/n2 --run-script game/game.lua
==21631== 
alsa init
==21631== 
==21631== HEAP SUMMARY:
==21631==     in use at exit: 68,593 bytes in 164 blocks
==21631==   total heap usage: 2,368 allocs, 2,204 frees, 1,461,915 bytes allocated
==21631== 
==21631== LEAK SUMMARY:
==21631==    definitely lost: 0 bytes in 0 blocks
==21631==    indirectly lost: 0 bytes in 0 blocks
==21631==      possibly lost: 0 bytes in 0 blocks
==21631==    still reachable: 68,593 bytes in 164 blocks
==21631==         suppressed: 0 bytes in 0 blocks
==21631== Reachable blocks (those to which a pointer was found) are not shown.
==21631== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==21631== 
==21631== For counts of detected and suppressed errors, rerun with: -v
==21631== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

nurpax avatar Jun 16 '20 20:06 nurpax

Please apply that patch to main :)

egandro avatar May 24 '21 16:05 egandro