pleriche / FastMM5

FastMM is a fast replacement memory manager for Embarcadero Delphi applications that scales well across multiple threads and CPU cores, is not prone to memory fragmentation, and supports shared memory without the use of external .DLL files.
290 stars 75 forks source link

UnregisterExpectedMemoryLeak not working #52

Open hafedh-trimeche opened 5 months ago

hafedh-trimeche commented 5 months ago

Hello, Please note that after setting this memory manager, pointers unregistered from Leak list still reported:

uses
  FastMM5;
var
  OldMM   : TMemoryManagerEx;
  FileName : string;

function NewRegisterMemoryLeak(P:Pointer):Boolean;
begin
  Result := OldMM.RegisterExpectedMemoryLeak(P);
end;

function NewUnregisterMemoryLeak(P:Pointer):Boolean;
begin
  Result := OldMM.UnregisterExpectedMemoryLeak(P);
end;

const
  NewMM : TMemoryManagerEx =
  (
    GetMem                       : NewAllocMem;
    FreeMem                      : NewFreeMem;
    ReallocMem                   : NewReallocMem;
    AllocMem                     : NewAllocMem;
    RegisterExpectedMemoryLeak   : NewRegisterMemoryLeak;
    UnregisterExpectedMemoryLeak : NewUnregisterMemoryLeak;
  );

initialization
  GetMemoryManager(OldMM);
  FileName := ChangeFileExt(ParamStr(0),'.leak');
  DeleteFile(FileName);
  FastMM_SetEventLogFilename(PChar(FileName));
  FastMM_OutputDebugStringEvents := FastMM_LogToFileEvents;
  FastMM_MessageBoxEvents        := [];
  FastMM_EnterDebugMode;
  SetMemoryManager(NewMM);
finalization
end.

MemoryModule and HookList are reported as leak when application terminated:

procedure InstallModule;
var
  Bytes : TBytes;
begin
  if Assigned(MemoryModule) or IsDesigntime then Exit;
  HookList          := TKeyRecord<TMemoryModule>.Create;

  Bytes             := ExtractResource('MEMORYMODULE.DLL');
  MemoryModule      := MemoryLoadLibary(Bytes);
  LoadLibraryMemory := MemoryGetProcAddress(MemoryModule,'LoadLibraryMemory');
  FreeLibraryMemory := MemoryGetProcAddress(MemoryModule,'FreeLibraryMemory');

  OldLoadLibrary    := HookProcedure(@LoadLibrary,@LoadLibraryHooked,LoadLibraryHook);
  OldLoadLibraryEx  := HookProcedure(@LoadLibraryEx,@LoadLibraryExHooked,LoadLibraryExHook);
  OldGetProcAddress := HookProcedure(@GetProcAddress,@GetProcAddressHooked,GetProcAddressHook);
  OldFreeLibrary    := HookProcedure(@FreeLibrary,@FreeLibraryHooked,FreeLibraryHook);

  UnregisterExpectedMemoryLeak(MemoryModule);
  UnregisterExpectedMemoryLeak(HookList);
end;

Best regards.