Is there a way to programmatically tell if particular block of memory was not freed by FastMM?

DelphiMemory LeaksDelphi 2009Fastmm

Delphi Problem Overview


I am trying to detect if a block of memory was not freed. Of course, the manager tells me that by dialog box or log file, but what if I would like to store results in a database? For example I would like to have in a database table a names of routines which allocated given blocks.

After reading a documentation of FastMM I know that since version 4.98 we have a possibility to be notified by manager about memory allocations, frees and reallocations as they occur. For example OnDebugFreeMemFinish event is passing to us a PFullDebugBlockHeader which contains useful informations. There is one thing that PFullDebugBlockHeader is missing - the information if the given block was freed by the application.

Unless OnDebugFreeMemFinish is called only for not freed blocks? This is which I do not know and would like to find out.

The problem is that even hooking into OnDebugFreeMemFinish event I was unable to find out if the block was freed or not.

Here is an example:

program MemLeakTest;

{$APPTYPE CONSOLE}

uses
  FastMM4, ExceptionLog, SysUtils;


procedure MemFreeEvent(APHeaderFreedBlock: PFullDebugBlockHeader; AResult: Integer);
begin
//This is executed at the end, but how should I know that this block should be freed
//by application? Unless this is executed ONLY for not freed blocks.
end;

procedure Leak;
var
  MyObject: TObject;
begin
  MyObject := TObject.Create;
end;

begin
  OnDebugFreeMemFinish := MemFreeEvent;
  Leak;
end.

What I am missing is the callback like:

procedure OnMemoryLeak(APointer: PFullDebugBlockHeader);

After browsing the source of FastMM I saw that there is a procedure:

procedure LogMemoryLeakOrAllocatedBlock(APointer: PFullDebugBlockHeader; IsALeak: Boolean);

which could be overriden, but maybe there is an easier way?

Delphi Solutions


Solution 1 - Delphi

Even if such handler exist, it would be nearly useless, as everything, including DB would be shut down at the time when FastMM reports leaks.

So, I suggest you to turn on LogErrorsToFile along with FullDebugMode conditionals in FastMM4Options.inc. This will give you a text file with leaks, which later you can parse and put into DB.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWodzuView Question on Stackoverflow
Solution 1 - DelphiSerhii KheilykView Answer on Stackoverflow