valgrind is overkill for tracing memory leaks. heaptrc is just as good if not better.
So -gl -gh or in short -glh
If you think though....
I don't fancy recompiling FPC (or the RTL) just because I need a longer stacktrace. Using valgrind I can specify it on the command line.
And yes, in many cases the amount of frames shown by heaptrc are enough. But not quite always.
And possible that FPC can cut off traces.
Using FPC 3.2.2 (as it comes, so NO debug info for RTL
and RTL compiled with -O2 ), below project compiled WITH no optimization and -gl and then either -gh or -gv
program Project1;
uses SysUtils, Classes;
var
s: TStringList;
function DoSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
allocmem(20);
end;
begin
s := TStringList.Create;
s.Add('1');
s.Add('2');
s.Add('3');
s.CustomSort(@DoSort);
s.Free;
end.
Heaptrc gets me
Heap dump by heaptrc unit of /tmp/project1
28 memory blocks allocated : 1939/1968
25 memory blocks freed : 1879/1896
3 unfreed memory blocks : 60
True heap size : 393216
True free heap : 392448
Should be : 392568
Call trace for block $00007F506696B200 size 20
$0000000000468F81
Call trace for block $00007F506696B100 size 20
$0000000000469009
Call trace for block $00007F506696B000 size 20
$0000000000469009
And valgrind
==2274==
==2274== HEAP SUMMARY:
==2274== in use at exit: 84 bytes in 3 blocks
==2274== total heap usage: 28 allocs, 25 frees, 2,163 bytes allocated
==2274==
==2274== 28 bytes in 1 blocks are definitely lost in loss record 1 of 2
==2274== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==2274== by 0x4269F1: CMEM_$$_CALLOCMEM$QWORD$$POINTER (in /tmp/project1)
==2274== by 0x466630: CLASSES$_$TSTRINGLIST_$__$$_QUICKSORT$LONGINT$LONGINT$TSTRINGLISTSORTCOMPARE (in /tmp/project1)
==2274== by 0x4230FB: SYSTEM_$$_SYSENTRY$TENTRYINFORMATION (in /tmp/project1)
==2274== by 0x40120F: ??? (in /tmp/project1)
==2274== by 0x401189: SI_C_$$_MAIN_STUB (in /tmp/project1)
==2274== by 0x4881082: (below main) (libc-start.c:308)
==2274==
==2274== 56 bytes in 2 blocks are definitely lost in loss record 2 of 2
==2274== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==2274== by 0x4269F1: CMEM_$$_CALLOCMEM$QWORD$$POINTER (in /tmp/project1)
==2274== by 0x4666B8: CLASSES$_$TSTRINGLIST_$__$$_QUICKSORT$LONGINT$LONGINT$TSTRINGLISTSORTCOMPARE (in /tmp/project1)
==2274== by 0x4230FB: SYSTEM_$$_SYSENTRY$TENTRYINFORMATION (in /tmp/project1)
==2274== by 0x40120F: ??? (in /tmp/project1)
==2274== by 0x401189: SI_C_$$_MAIN_STUB (in /tmp/project1)
==2274== by 0x4881082: (below main) (libc-start.c:308)
==2274==
==2274== LEAK SUMMARY:
==2274== definitely lost: 84 bytes in 3 blocks
==2274== indirectly lost: 0 bytes in 0 blocks
==2274== possibly lost: 0 bytes in 0 blocks
==2274== still reachable: 0 bytes in 0 blocks
==2274== suppressed: 0 bytes in 0 blocks
==2274==
==2274== For lists of detected and suppressed errors, rerun with: -s
==2274== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Mind, both of them skip "DoSort" due to optimizations in the RTL by FPC.
Change it to
procedure Bar();
begin
allocmem(20);
end;
function DoSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
bar;
end;
And "Bar" will be skipped, but DoSort will be shown.
HeapTrc:
Call trace for block $00007F44A2EA4300 size 20
$00000000004010C8 DoSort, line 14 of project1.lpr
$0000000000468F91
and valgrind
==2332== 28 bytes in 1 blocks are definitely lost in loss record 1 of 2
==2332== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==2332== by 0x4269D1: CMEM_$$_CALLOCMEM$QWORD$$POINTER (in /tmp/project1)
==2332== by 0x401257: P$PROJECT1_$$_DOSORT$TSTRINGLIST$LONGINT$LONGINT$$LONGINT (project1.lpr:13)
==2332== by 0x466610: CLASSES$_$TSTRINGLIST_$__$$_QUICKSORT$LONGINT$LONGINT$TSTRINGLISTSORTCOMPARE (in /tmp/project1)
==2332== by 0x4230DB: SYSTEM_$$_SYSENTRY$TENTRYINFORMATION (in /tmp/project1)
==2332== by 0x40120F: ??? (in /tmp/project1)
==2332== by 0x401189: SI_C_$$_MAIN_STUB (in /tmp/project1)
==2332== by 0x4881082: (below main) (libc-start.c:308)
I am not sure why valgrind didn't use the demangled name.... (test was done on an older Linux, with older valgrind 3.15