Closed jvo203 closed 3 months ago
My recollection is that we spent a lot of time on valgrind in the past and many of these possible leaks are not really leaks but are caused by AST having an internal memory pool that valgrind can't track. We had to use an internal malloc implementation because of all the small structs that are continually created and freed by AST.
This is why we have an explicit option to turn off the memory allocator if you want to use memory debugging tools
https://github.com/Starlink/ast/blob/master/configure.ac#L65-L72
Thank you for a prompt response. I'll try to use the --with-memdebug
option and see what happens. Anyway, it very well could be that valgrind is not aware of the custom memory "hocus pocus" that AST does internally.
Hmm, I re-compiled the AST with ./configure --prefix=/usr/local --with-memdebug
but valgrind still reports a problem...
int main()
{
astBegin;
astEnd;
return 0;
}
chris@capricorn:~/projects/FITSWEBQLSE/tests> valgrind --leak-check=full --show-leak-kinds=all --suppressions=/usr/share/glib-2.0/valgrind/glib.supp ./test_starlink
==28384== Memcheck, a memory error detector
==28384== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==28384== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==28384== Command: ./test_starlink
==28384==
==28384==
==28384== HEAP SUMMARY:
==28384== in use at exit: 139,840 bytes in 3 blocks
==28384== total heap usage: 4 allocs, 1 frees, 139,940 bytes allocated
==28384==
==28384== 16 bytes in 1 blocks are still reachable in loss record 1 of 3
==28384== at 0x4845794: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28384== by 0x493DD43: astGlobalsInit_ (globals.c:234)
==28384== by 0x4BB16C4: astBegin_ (object.c:6601)
==28384== by 0x401AEE: main (in /mnt/data/chris/projects/FITSWEBQLSE/tests/test_starlink)
==28384==
==28384== 104 bytes in 1 blocks are still reachable in loss record 2 of 3
==28384== at 0x484CC4C: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28384== by 0x4B876A1: astRealloc_ (memory.c:3744)
==28384== by 0x4BB166D: astBegin_ (object.c:6620)
==28384== by 0x401AEE: main (in /mnt/data/chris/projects/FITSWEBQLSE/tests/test_starlink)
==28384==
==28384== 139,720 bytes in 1 blocks are still reachable in loss record 3 of 3
==28384== at 0x4845794: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28384== by 0x493D99F: astGlobalsInit_ (globals.c:138)
==28384== by 0x4BB16C4: astBegin_ (object.c:6601)
==28384== by 0x401AEE: main (in /mnt/data/chris/projects/FITSWEBQLSE/tests/test_starlink)
==28384==
==28384== LEAK SUMMARY:
==28384== definitely lost: 0 bytes in 0 blocks
==28384== indirectly lost: 0 bytes in 0 blocks
==28384== possibly lost: 0 bytes in 0 blocks
==28384== still reachable: 139,840 bytes in 3 blocks
==28384== suppressed: 0 bytes in 0 blocks
==28384==
==28384== For lists of detected and suppressed errors, rerun with: -s
==28384== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Calling test_fk5()
results in an increase in the reachable memory + the usual "errors"...
==28445== 432 bytes in 1 blocks are still reachable in loss record 75 of 77
==28445== at 0x4845794: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28445== by 0x4B8705D: astMalloc_ (memory.c:2927)
==28445== by 0x4BAF764: astInitObject_ (object.c:5617)
==28445== by 0x4B43FE9: astInitMapping_ (mapping.c:23894)
==28445== by 0x4923C56: astInitFrame_ (frame.c:14879)
==28445== by 0x4CF1759: astInitTimeFrame_ (timeframe.c:7053)
==28445== by 0x4CF18B7: astTimeFrame_ (timeframe.c:6942)
==28445== by 0x4CA8FCD: CalcLAST (skyframe.c:1294)
==28445== by 0x4CAAAC0: SetLast.part.0 (skyframe.c:9340)
==28445== by 0x4CAAB22: SetLast (skyframe.c:9334)
==28445== by 0x4CAAB22: GetLAST (skyframe.c:3837)
==28445== by 0x4CB3893: MakeSkyMapping (skyframe.c:6065)
==28445== by 0x4CB3893: SubFrame (skyframe.c:10062)
==28445== by 0x4928275: Match (frame.c:7332)
==28445==
==28445== 432 bytes in 1 blocks are still reachable in loss record 76 of 77
==28445== at 0x4845794: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28445== by 0x4B8705D: astMalloc_ (memory.c:2927)
==28445== by 0x4BAF764: astInitObject_ (object.c:5617)
==28445== by 0x4B43FE9: astInitMapping_ (mapping.c:23894)
==28445== by 0x4923C56: astInitFrame_ (frame.c:14879)
==28445== by 0x4CF1759: astInitTimeFrame_ (timeframe.c:7053)
==28445== by 0x4CF18B7: astTimeFrame_ (timeframe.c:6942)
==28445== by 0x4CA8FE5: CalcLAST (skyframe.c:1295)
==28445== by 0x4CAAAC0: SetLast.part.0 (skyframe.c:9340)
==28445== by 0x4CAAB22: SetLast (skyframe.c:9334)
==28445== by 0x4CAAB22: GetLAST (skyframe.c:3837)
==28445== by 0x4CB3893: MakeSkyMapping (skyframe.c:6065)
==28445== by 0x4CB3893: SubFrame (skyframe.c:10062)
==28445== by 0x4928275: Match (frame.c:7332)
==28445==
==28445== 139,720 bytes in 1 blocks are still reachable in loss record 77 of 77
==28445== at 0x4845794: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==28445== by 0x493D99F: astGlobalsInit_ (globals.c:138)
==28445== by 0x4BB16C4: astBegin_ (object.c:6601)
==28445== by 0x401AEE: main (in /mnt/data/chris/projects/FITSWEBQLSE/tests/test_starlink)
==28445==
==28445== LEAK SUMMARY:
==28445== definitely lost: 0 bytes in 0 blocks
==28445== indirectly lost: 0 bytes in 0 blocks
==28445== possibly lost: 0 bytes in 0 blocks
==28445== still reachable: 149,624 bytes in 77 blocks
==28445== suppressed: 0 bytes in 0 blocks
==28445==
==28445== For lists of detected and suppressed errors, rerun with: -s
==28445== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
I really do hope these are "phantom" problems caused by custom memory management by AST ...
The --with-memdebug option only enables the internal AST memory debugging tools - it doesn't effect errors reported by valgrind in any way. As Tim says, AST allocates various memory blocks which it never releases. But these allocations should only happen once per activation of an AST-based application. So as the application becomes big (in terms of AST usage) the memory leaks shown by valgrind should not increases - significantly. Putting AST code inside a loop is a good way to test this. The internal AST memory debugging tools can be used to track down memory leaks within AST if it does look like the memory used by the application is increasing in proportion to the number of AST calls. To use them, configure --with-memdebug, and then include "astFlushMemory(1)" at the end of the application code. This will cause AST to report the internal memory identifiers for any allocated memory blocks that have not been released (it does not report blocks that are deemed as permanent memory blocks that AST never releases). The astFlushMemory function is defined and documented in source file src/memory.c.
Thank you. Indeed I ran a simple loop the other day and there was no increase in "still reachable" memory irrespective of the number of iterations. Valgrind always reported still reachable: 139,736 bytes in 2 blocks
, which must be some kind of a permanent memory pool pre-allocated by AST.
Let's start with an empty "do-nothing" C program:
Compilation step:
cc
pkg-config --cflags cfitsio-I/home/chris/Downloads/wcssubs -o test_starlink test_starlink.c -L/usr/local/lib
ast_link` Valgrind command:
valgrind --leak-check=full --show-leak-kinds=all --suppressions=/usr/share/glib-2.0/valgrind/glib.supp ./test_starlink` A clean output from valgrind:Next let's add
astBegin
andandEnd
with nothing else:There are now "still reachable" blocks:
It gets progressively worse upon actually using AST to do something:
Valgrind reports plenty of memory errors. The list is rather long. What do you think? OS is 64-bit openSUSE Tumbleweed with
gcc (SUSE Linux) 13.3.0
.