T238: "ssh_init" and "ssh_finalize" leaks memory
Description
Originally reported by matick: https://bugs.libssh.org/T238
Whenever ssh_init is called, function "ssh_mutex_lock" will allocate memory for mutex, but this memory is never released. I would expect that mutex "ssh_init_mutex" is released upon "ssh_finalize", but it is not.
Comments:
Jakuje commented on 2020-08-12 11:37:26 UTC:
This looks like Windows-only issue. If the pthreads on Linux are used, the mutex is initialized statically, which is probably a reason why it does not pop up for us. It looks like windows locks do not support static initialization.
At this moment, there s no simple way to free this particlar mutex, but given that it is really one-off allocation when libssh is initialized, one-off leak during finalization and this memory is not growing throughout the usage it is very low priority to solve now.
matick commented on 2020-08-12 11:54:15 UTC:
I guess that if in function "static int _ssh_finalize(unsigned destructor)" this: if (!destructor) { ssh_mutex_unlock(&ssh_init_mutex); } return 0; would be replaced with this: if (!destructor) { ssh_mutex_unlock(&ssh_init_mutex); if (!_ssh_initialized) { free(ssh_init_mutex); ssh_init_mutex = SSH_MUTEX_STATIC_INIT; } } return 0;
It should work just fine without leaks.
Jakuje commented on 2020-08-12 13:45:17 UTC:
On windows yes, but it would fail everywhere else, where ssh_init_mutex
is static mutex. Feel free to submit your suggested change in the gitlab -- it should run it through the CI to check if it works:
https://gitlab.com/libssh/libssh-mirror