有没有办法以编程方式判断特定内存块是否未被 FastMM 释放?
我正在尝试检测是否有一块内存未被释放。当然,经理通过对话框或日志文件告诉我,但是如果我想将结果存储在数据库中怎么办?例如,我想在数据库表中包含分配给定块的例程的名称。
在阅读了 FastMM 的文档后,我知道从 4.98 版开始,我们有可能在内存分配、释放和重新分配发生时收到管理器的通知。例如,OnDebugFreeMemFinish
事件正在向我们传递一个包含有用信息的 PFullDebugBlockHeader
。 PFullDebugBlockHeader
缺少一件事 - 给定块是否已被应用程序释放的信息。
除非仅针对未释放的块调用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);
可以被覆盖,但也许有更简单的方法?
I am trying to detect if a block of memory was not freed. Of course, the manager tells me that by dialog box or log file, but what if I would like to store results in a database? For example I would like to have in a database table a names of routines which allocated given blocks.
After reading a documentation of FastMM I know that since version 4.98 we have a possibility to be notified by manager about memory allocations, frees and reallocations as they occur. For example OnDebugFreeMemFinish
event is passing to us a PFullDebugBlockHeader
which contains useful informations.
There is one thing that PFullDebugBlockHeader
is missing - the information if the given block was freed by the application.
Unless OnDebugFreeMemFinish
is called only for not freed blocks? This is which I do not know and would like to find out.
The problem is that even hooking into OnDebugFreeMemFinish
event I was unable to find out if the block was freed or not.
Here is an example:
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.
What I am missing is the callback like:
procedure OnMemoryLeak(APointer: PFullDebugBlockHeader);
After browsing the source of FastMM I saw that there is a procedure:
procedure LogMemoryLeakOrAllocatedBlock(APointer: PFullDebugBlockHeader; IsALeak: Boolean);
which could be overriden, but maybe there is an easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使存在这样的处理程序,它也几乎毫无用处,因为当 FastMM 报告泄漏时,包括数据库在内的所有内容都将被关闭。
因此,我建议您在
FastMM4Options.inc
中打开LogErrorsToFile
以及FullDebugMode
条件。这将为您提供一个带有泄漏的文本文件,稍后您可以解析该文件并将其放入数据库中。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 withFullDebugMode
conditionals inFastMM4Options.inc
. This will give you a text file with leaks, which later you can parse and put into DB.