Delphi   发布时间:2022-04-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了delphi – 如何配置FastMM以检测dll中的内存泄漏大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法弄清楚如何检测静态或甚至动态链接的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来解决此问题.

大佬总结

以上是大佬教程为你收集整理的delphi – 如何配置FastMM以检测dll中的内存泄漏全部内容,希望文章能够帮你解决delphi – 如何配置FastMM以检测dll中的内存泄漏所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。