加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:20:19 所属栏目:大数据 来源:网络整理
导读:我无法弄清楚如何检测静态或甚至动态链接的DLL中的内存泄漏.我只想检测dll中的泄漏,我不想在dll和应用程序之间共享内存管理器.另外,dll与运行时包链接 我的示例dll看起来像这样: library dll;uses fastmm4,System.SysUtils,System.Classes;{$R *.res}proced
我无法弄清楚如何检测静态或甚至动态链接的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)引起的memleak,而不是dll中的泄漏.

并在应用程序主单元中:

unit main;

interface

uses
  Winapi.Windows,Winapi.Messages,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,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位于输出目录中.

我创造了一个repository on github.我错过了什么?

解决方法

您的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来解决此问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读