为了消除所有内存泄漏,我试图将LeakCheck库合并到 DUnit 中。问题是 LeakCheck 会向我报告不是真正泄漏的内存泄漏。这些是由 RTL 分配并在程序退出时销毁的对象或其他东西。
LeakCheck 包含许多允许您指定要忽略的例程,我能够使用它们来忽略大多数此类“泄漏”。但是,我不知道如何特别摆脱一个:
program LeakCheckMemLeak;
{$APPTYPE CONSOLE}
uses
LeakCheck, TestFramework, LeakCheck.DUnit, LeakCheck.Utils, LeakCheck.Setup.Trace, System.SysUtils,
Forms, System.Classes;
{$R *.RES}
procedure LeakMemory;
var
LThread: TThread;
begin
LThread := TThread.Create(True);
LThread.Free;
end;
procedure DetectLeak;
var
Snapshot: TLeakCheck.TSnapshot;
Report: LeakString;
begin
Snapshot.Create;
LeakMemory;
Report := TLeakCheck.GetReport(Snapshot.Snapshot);
try
Writeln(string(Report));
finally
Report.Free;
end;
end;
begin
Application.Initialize;
DetectLeak;
Readln;
end.
创建实例TThread
(最初是,TThread.CreateAnonymousThread
但结果相同)会导致 64 字节的内存泄漏:
Total allocation count: 297 (12592 B)
Leak detected 02BB3DC0 size 64 B
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E8 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ????????????????????????????????
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ????????????????????????????????
Stack trace when the memory block was allocated:
$00406E76 - LeakCheckMemLeak.exe - System.AllocMem + $A (4562 +25)
$004C4A53 - LeakCheckMemLeak.exe - System.Classes.TThread.WaitFor + $8F (15565 +23)
$005E4EB0 - LeakCheckMemLeak.exe - LeakCheckMemLeak.DetectLeak + $34 (24 +3)
$005ED5B9 - LeakCheckMemLeak.exe - LeakCheckMemLeak.LeakCheckMemLeak + $29 (35 +3)
$763E343D - kernel32.dll
$76F19832 - ntdll.dll
在其他情况下,LeakCheck 为我提供了该实例泄漏的类的名称,因此我可以将其添加到忽略列表中,但在这种情况下它没有。我怎样才能抑制这种“泄漏”?
附带说明一下,我遇到的这个和其他泄漏不会发生在 GUI 应用程序中。我猜,RTL 在测试运行之前会为这些对象中的大多数预先分配内存。