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

bal*_*azs 7 delphi dll fastmm delphi-xe3

我无法弄清楚如何检测静态或甚至动态链接的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.
Run Code Online (Sandbox Code Playgroud)

应用程序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.
Run Code Online (Sandbox Code Playgroud)

注意:如果我取消注释fastmm4,那么我可以检测到由应用程序(TStringList.Create)引起的memleak,而不是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.
Run Code Online (Sandbox Code Playgroud)

我希望FastMM在调用FreeLibrary时生成报告,或者在程序退出时生成报告,如果dll是静态加载的,但没有任何反应.

FastMM4Options.inc我另外只是设置FullDebugModeClearLogFileOnStartup,并且FastMM_FullDebugMode.dll在输出目录中.

在github上创建了一个存储库.我错过了什么?

Dav*_*nan 5

您的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}
  );
Run Code Online (Sandbox Code Playgroud)

在您的选项中,RequireDebuggerPresenceForLeakReporting已定义.而且,在DLL中,DebugHook等于0,大概是因为你正在调试应用程序而不是DLL.这意味着你打电话CheckBlocksOnShutdown通过False.这False会禁用泄漏报告.

您可以通过取消定义来解决此问题RequireDebuggerPresenceForLeakReporting.