Hast du mit valgrind support kompiliert (Projektoptionen->Debugging->-vg switch)?
Beispiel:
Code: Alles auswählen
➜ cat test.pas
var
p: Pointer;
begin
p:=GetMem(1024);
WriteLn(IntPtr(p));
end.
➜ fpc -g -gv ./test.pas
Free Pascal Compiler version 3.2.2 [2024/05/21] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling ./test.pas
Linking test
6 lines compiled, 0.1 sec
➜ valgrind ./test
==4692== Memcheck, a memory error detector
==4692== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==4692== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==4692== Command: ./test
==4692==
78188616
==4692==
==4692== HEAP SUMMARY:
==4692== in use at exit: 1,032 bytes in 1 blocks
==4692== total heap usage: 1 allocs, 0 frees, 1,032 bytes allocated
==4692==
==4692== LEAK SUMMARY:
==4692== definitely lost: 0 bytes in 0 blocks
==4692== indirectly lost: 0 bytes in 0 blocks
==4692== possibly lost: 0 bytes in 0 blocks
==4692== still reachable: 1,032 bytes in 1 blocks
==4692== of which reachable via heuristic:
==4692== length64 : 1,032 bytes in 1 blocks
==4692== suppressed: 0 bytes in 0 blocks
==4692== Rerun with --leak-check=full to see details of leaked memory
==4692==
==4692== For lists of detected and suppressed errors, rerun with: -s
==4692== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Das Speicherleck wird als "still reachable" kategorisiert weil p ein globale Variable ist, die bis zum ende der Programmlaufzeit gültig ist. Wenn ichs in ne Lokale Variable verschiebe:
Code: Alles auswählen
procedure Test;
var
p: Pointer;
begin
p:=GetMem(1024);
WriteLn(IntPtr(p));
end;
begin
Test;
end.
Bekomm ich:
Code: Alles auswählen
==5367== Memcheck, a memory error detector
==5367== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==5367== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==5367== Command: ./test
==5367==
78188616
==5367==
==5367== HEAP SUMMARY:
==5367== in use at exit: 1,032 bytes in 1 blocks
==5367== total heap usage: 1 allocs, 0 frees, 1,032 bytes allocated
==5367==
==5367== LEAK SUMMARY:
==5367== definitely lost: 1,032 bytes in 1 blocks
==5367== indirectly lost: 0 bytes in 0 blocks
==5367== possibly lost: 0 bytes in 0 blocks
==5367== still reachable: 0 bytes in 0 blocks
==5367== suppressed: 0 bytes in 0 blocks
==5367== Rerun with --leak-check=full to see details of leaked memory
==5367==
==5367== For lists of detected and suppressed errors, rerun with: -s
==5367== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Jetzt ists definitely lost