7

静的または動的にリンクされた 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) が原因のメモリリークは検出できますが、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.

dll が静的に読み込まれている場合、FreeLibrary が呼び出されたとき、またはプログラムの終了時に FastMM からレポートが生成されることを期待していますが、何も起こりません。

さらに、FullDebugModeClearLogFileOnStartupFastMM4Options.incを設定しただけで、FastMM_FullDebugMode.dllが出力ディレクトリにあります。

github にリポジトリを作成しました。私は何が欠けていますか?

4

2 に答える 2

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

あなたのオプションでRequireDebuggerPresenceForLeakReportingは、定義されています。さらに、DLL では、おそらく DLL ではなくアプリケーションをデバッグしているため、DebugHookは と等しくなります。これは、 passing0を呼び出すことを意味します。そして、それはリークの報告を無効にします.CheckBlocksOnShutdownFalseFalse

これは、定義を解除することで解決できますRequireDebuggerPresenceForLeakReporting

于 2013-07-05T10:26:10.340 に答える
-1

Delphi2010-win7のバージョンFast Memory Manager 4.97でテストしました

  1. FastMM4 は、.dpr (プロジェクトと dll) の「uses」句の最初のユニットです。
  2. 「ShareMM」オプションが有効になっています
  3. 「AttemptToUseSharedMM」オプションが有効になっています
  4. 「EnableMemoryLeakReporting」オプションが有効になっています

exeのフォルダにFastMM_FullDebugMode.dllを追加

テスト デモ 'Dynamically Loaded DLL' もあります。このデモには ShareMem がありません。オプション 'ShareMM' と 'AttemptToUseSharedMM' を有効に設定し、FastMM_FullDebugMode.dll を追加して、FastMM のリーク レポートを取得する必要があります。

于 2013-07-05T08:04:10.110 に答える