How to configure FastMM to detect memory leak in dll

I cannot figure out how to detect memory leaks in a statically or even dynamically linked dll. I just want to detect leaks in the dll, and I don't want to share the memory manager between the dll and the application. In addition, the dll is associated with runtime packages.

My dll sample looks like this:

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

application 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. 

Note. If I uncomment fastmm4, I can detect the memleak caused by the application (TStringList.Create), but not a leak in the dll.

And in the main block of the application:

 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. 

I expect FastMM to generate a report when calling FreeLibrary or when exiting a program if the DLL is statically loaded but nothing happens.

In FastMM4Options.inc I also just installed FullDebugMode and ClearLogFileOnStartup , and FastMM_FullDebugMode.dll is in the output directory.

I created a repository on github . What am I missing?

+7
dll delphi delphi-xe3 fastmm
source share
2 answers

The reason your DLL does not report leaks comes from this code when FastMM completes:

  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} ); 

Your settings define RequireDebuggerPresenceForLeakReporting . What else, in a DLL, DebugHook is 0 , presumably because you are debugging the application, not the DLL. This means that you are calling CheckBlocksOnShutdown through False . And that False disables leak reporting.

You can solve this problem by setting RequireDebuggerPresenceForLeakReporting .

+5
source share

I am just testing it with the Fast Memory Manager version 4.97 on Delphi2010 - win7

  • FastMM4 is the first unit in the section 'uses'.dpr (project and dll)
  • The option "ShareMM" is enabled.
  • 'AttemptToUseSharedMM' is included.
  • EnableMemoryLeakReporting option enabled

Add FastMM_FullDebugMode.dll to exe folder

There is also a test demo "Dynamically loaded DLL", This is a demo without ShareMem. I have to set the “ShareMM” and “AttemptToUseSharedMM” parameters and add FastMM_FullDebugMode.dll to have a FastMM leak report.

-one
source share

All Articles