如何配置FastMM以便在dll中检测内存泄漏

7

我不知道如何检测静态或动态链接的DLL中的内存泄漏。我只想检测DLL中的泄漏,而且我不想在DLL和应用程序之间共享内存管理器。此外,该DLL与运行时包相链接。

我的示例DLL如下所示:

library dll;
uses
  fastmm4,
  System.SysUtils,
  System.Classes;
{$R *.res}
procedure MyInit; stdcall;
Begin
  TObject.Create;
End;
exports MyInit;
begin
end.

应用程序 dpr:

program app;

uses
  //fastmm4,
  Vcl.Forms,
  main in 'main.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

注意:如果我取消注释 fastmm4,那么我可以检测到由应用程序(TStringList.Create)引起的内存泄漏,但无法检测到 dll 中的泄漏。
而在应用程序主单元中:
unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    LDLLHandle: HModule;
    LShowProc: TProcedure;
  end;

var
  Form1: TForm1;

{$ifdef static}
procedure MyInit; stdcall; external 'dll.dll';
{$endif}

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TStringList.Create;
  {$ifdef static}
  MyInit;
  {$else}
  LDLLHandle := LoadLibrary('dll.dll');
  if LDLLHandle <> 0 then
  begin
    try
      LShowProc := GetProcAddress(LDLLHandle, 'MyInit');
      if Assigned(LShowProc) then
        LShowProc;
    finally
      FreeLibrary(LDLLHandle);
    end;
  end;
  {$endif}
end;

end.

我希望FastMM在调用FreeLibrary或程序退出时生成报告,如果dll是静态加载的,则不会发生任何事情。
在FastMM4Options.inc中,我另外设置了FullDebugMode和ClearLogFileOnStartup,并且FastMM_FullDebugMode.dll位于输出目录中。
我创建了一个在github上的存储库repository on github。我缺少什么?

奇怪...你只是克隆了仓库,然后在XE3上运行,它就能工作了吗? - balazs
我在构建自己的项目时无法重现此问题,但我使用了自己的fastmm选项。然而,当我拿到你的项目后,我能够重现该问题,并已经解决了它。 - David Heffernan
2个回答

6
你的 DLL 没有报告泄漏的原因是由于 FastMM 关闭时的此代码:
  CheckBlocksOnShutdown(
  {$ifdef EnableMemoryLeakReporting}
        True
    {$ifdef RequireIDEPresenceForLeakReporting}
        and DelphiIsRunning
    {$endif}
    {$ifdef RequireDebuggerPresenceForLeakReporting}
        and ((DebugHook <> 0)
        {$ifdef PatchBCBTerminate}
        or (Assigned(pCppDebugHook) and (pCppDebugHook^ <> 0))
        {$endif PatchBCBTerminate}
        )
    {$endif}
    {$ifdef ManualLeakReportingControl}
        and ReportMemoryLeaksOnShutdown
    {$endif}
  {$else}
        False
  {$endif}
  );

在您的选项中,定义了RequireDebuggerPresenceForLeakReporting。此外,在DLL中,DebugHook等于0,可能是因为您正在调试应用程序而不是DLL。这意味着您调用CheckBlocksOnShutdown并传递False。而那个False禁用了泄漏报告。
您可以通过取消定义RequireDebuggerPresenceForLeakReporting来解决此问题。

我使用 RequireDebuggerPresenceForLeakReporting Disable、ShareMM Disable 和 AttemtToUseSharedMM Disable,获取 fastMM 的泄漏报告。 - Ravaut123
@Ravaut123 正如我在答案中所述,阻止 Balazs 项目中泄漏报告的单一因素是定义了 RequireDebuggerPresenceForLeakReporting - David Heffernan
谢谢,它与静态和动态加载的dll一样完美地工作。 - balazs

-1

我刚在 Delphi2010 - win7 上使用 Fast Memory Manager 4.97 版本进行了测试。

  1. FastMM4 是 .dpr(项目和 dll)中 'uses' 子句中的第一个单元。
  2. 'ShareMM' 选项已启用。
  3. 'AttemptToUseSharedMM' 选项已启用。
  4. 'EnableMemoryLeakReporting' 选项已启用。

将 FastMM_FullDebugMode.dll 添加到 exe 文件所在的文件夹中。

还有一个名为“动态加载 DLL”的测试演示。 此演示没有使用 ShareMem。 我必须设置 'ShareMM' 和 'AttemptToUseSharedMM' 选项,并添加 FastMM_FullDebugMode.dll 才能获得 FastMM 的泄漏报告。


你为什么启用了Sharemem?请考虑到提问者明确表示他不想使用Sharemem。 - David Heffernan
我的测试DLL项目是使用Memshare制作的,所以我也在上面测试了Memshare。 - Ravaut123
这对你来说很好。但是为什么不尝试匹配问题描述中的场景呢? - David Heffernan
同时测试FastMM的演示程序“Dynamically Loaded DLL”。这也是工作的一部分。不使用ShareMem;-) - Ravaut123
@balazs:我在我的项目或dll中没有声明ShareMem。我只是在FastMM的选项中设置它为启用状态。 - Ravaut123
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接