Hello Cython-users,
I have been struggling with -potential- memory leak detection within my cython codes. I would like to use valgrind to detect leaks, if there is any. So I followed the guidelines provided in
[1] and uncommented the recommended lines (valgrind-python.supp is attached). For illustration, I did a simple test to compare memory leak detection of valgrind between a C code and Cython (where a Cython module is explicitly imported).
Here is the naive C code:
#include <stdlib.h> int main(){
double *x;
x= malloc(1000*sizeof(double));
//free(x); //When uncommented the error disappears confirming that memory is freed
return 0;}
Compiled it with gcc 4.8.3 on x64 SL7:
gcc -g -o mem_leak mem_leak.c
After that, I ran valgrind with:
valgrind --tool=memcheck --leak-check=full ./mem_leak
Provided the following output as expected complaining that I have memory leak if 8,000 bytes:
.....
.....
==16119== HEAP SUMMARY:
==16119== in use at exit: 8,000 bytes in 1 blocks
==16119== total heap usage: 1 allocs, 0 frees, 8,000 bytes allocated
==16119==
==16119== Searching for pointers to 1 not-freed blocks
==16119== Checked 75,320 bytes
==16119==
==16119== 8,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==16119== at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==16119== by 0x400541: main (mem_leak.c:5)
==16119==
==16119== LEAK SUMMARY:
==16119== definitely lost: 8,000 bytes in 1 blocks
==16119== indirectly lost: 0 bytes in 0 blocks
==16119== possibly lost: 0 bytes in 0 blocks
==16119== still reachable: 0 bytes in 0 blocks
==16119== suppressed: 0 bytes in 0 blocks
==16119==
==16119== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--16119--
--16119-- used_suppression: 2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a /usr/lib64/valgrind/default.supp:1296
==16119==
==16119== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Then I use a malloc in a Cython code called, which is called from a python code:
from cymem_leak import testmain
And below cython code is of course compiled with option -g as well added into the setup.py file:
from libc.stdlib cimport malloc, free,abort
x= <double*> malloc(sizeof(double) * 1000)
#free(x) #Whether commented or uncommented, valgrind output does not change at all
After compilation I ran valgrind for this cython code with:
valgrind --tool=memcheck --leak-check=full --suppressions=valgrind-python.supp python ./pymem_leak.py
One would expect that I should get a similar error as I did with C, but, I do not get any specific error related to this memory leak only a long list of errors (full output is attached):
....
.....
==16190== definitely lost: 0 bytes in 0 blocks
==16190== indirectly lost: 0 bytes in 0 blocks
==16190== possibly lost: 280,279 bytes in 1,025 blocks
==16190== still reachable: 591,289 bytes in 4,288 blocks
==16190== suppressed: 0 bytes in 0 blocks
==16190== Reachable blocks (those to which a pointer was found) are not shown.
==16190== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==16190== For counts of detected and suppressed errors, rerun with: -v
==16190== ERROR SUMMARY: 300 errors from 300 contexts (suppressed: 2 from 2)
The bottomline is, either I am doing something incomplete, or it seems it is not possible to identify the memoryleaks in Cython by using valgrind. I would greatly appreciate your help. Thank you
S. Golge