有没有一种方法可以通过编程来判断 FastMM 是否释放了特定的内存块?

我试图检测一个内存块是否被释放。当然,管理器通过对话框或日志文件告诉我这一点,但是如果我想将结果存储在数据库中该怎么办呢?例如,我希望在数据库表中有一个分配给定块的例程的名称。

在阅读了 FastMM 的文档之后,我知道自从4.98版以来,我们有可能在内存分配、释放和重新分配发生时得到管理员的通知。例如,OnDebugFreeMemFinish事件传递给我们一个包含有用信息的 PFullDebugBlockHeaderPFullDebugBlockHeader缺少一样东西——应用程序释放给定块时的信息。

除非仅对未释放的块调用 OnDebugFreeMemFinish?这是我不知道,想要找出来。

问题是,即使连接到 OnDebugFreeMemFinish事件,我也无法知道块是否被释放。

这里有一个例子:

program MemLeakTest;


{$APPTYPE CONSOLE}


uses
FastMM4, ExceptionLog, SysUtils;




procedure MemFreeEvent(APHeaderFreedBlock: PFullDebugBlockHeader; AResult: Integer);
begin
//This is executed at the end, but how should I know that this block should be freed
//by application? Unless this is executed ONLY for not freed blocks.
end;


procedure Leak;
var
MyObject: TObject;
begin
MyObject := TObject.Create;
end;


begin
OnDebugFreeMemFinish := MemFreeEvent;
Leak;
end.

我缺少的是这样的回复:

procedure OnMemoryLeak(APointer: PFullDebugBlockHeader);

在浏览了 FastMM 的源代码之后,我发现有一个过程:

procedure LogMemoryLeakOrAllocatedBlock(APointer: PFullDebugBlockHeader; IsALeak: Boolean);

可以重写,但也许有更简单的方法?

2714 次浏览

Even if such handler exist, it would be nearly useless, as everything, including DB would be shut down at the time when FastMM reports leaks.

So, I suggest you to turn on LogErrorsToFile along with FullDebugMode conditionals in FastMM4Options.inc. This will give you a text file with leaks, which later you can parse and put into DB.