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

前端之家收集整理的这篇文章主要介绍了delphi – 如何配置FastMM以检测dll中的内存泄漏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法弄清楚如何检测静态或甚至动态链接的DLL中的内存泄漏.我只想检测dll中的泄漏,我不想在dll和应用程序之间共享内存管理器.另外,dll与运行时包链接

我的示例dll看起来像这样:

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

应用程序dpr:

@H_301_4@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中的泄漏.

并在应用程序主单元中:

@H_301_4@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关闭中的此代码: @H_301_4@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相关文章