Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide expected memory leaks in FastMM?

I have following sample application that shows the issue:

program FalseMemLeak;

uses
  ShareMem;

var
  o: TObject;
begin
  o := TObject.Create; // "good" leak
  RegisterExpectedMemoryLeak(o);
  TInterfacedObject.Create; // bad leak
end.

I am now using the BorlndMM.dll replacement and the FastMMFullDebug.dll and I get following report:

---------------------------
FalseMemLeak.exe: Memory Leak Detected
---------------------------
This application has leaked memory. The small block leaks are:

5 - 12 bytes: TObject x 1
13 - 20 bytes: TInterfacedObject x 1

---------------------------
OK   
---------------------------

When I remove the "bad" memory leak everything is fine and no report is shown. But as soon as there is some unexpected memory leak it also lists the registered leaks.

Originally I found this when I was looking for these Indy memory leaks and found out they are registered but still reported amongst those that are real memory leaks.

When I am using the built-in ReportMemoryLeaksOnShutdown := True it only reports the leak for TInterfacedObject.

So is there a way to filter out the registered memory leaks when using FastMM in full debug mode?


To make this clear: This is the BorlndMM.dll that comes with the FastMM zip and which states that this is the replacement for the out of the box one and it uses FastMM4 and loads the FastMM_FullDebugMode.dll. All calls to the memory manager are thus handled by FastMM4. But somehow it seems to ignore filtering out the registered leaks (which are registered with FastMM inside the replacement BorlndMM.dll - that can be seen when debugging that DLL). Yes the registered leaks are not reported when using FastMM4.pas but changing that is not up for debate.

like image 744
Stefan Glienke Avatar asked Feb 23 '15 17:02

Stefan Glienke


People also ask

How can we avoid memory leak?

To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

Do memory leaks go away when you close the program?

So technically the program terminates, but because it still resides on memory, any memory leak would not be released unless you unload the program.


1 Answers

In FastMM4Options.inc there is the following:

{$ifdef borlndmmdll}
  ....
  {$undef HideExpectedLeaksRegisteredByPointer}
....

The undefining of HideExpectedLeaksRegisteredByPointer is what is causing the behaviour that you observe. Recompile the replacement borlandmm.dll with HideExpectedLeaksRegisteredByPointer defined and your expected leaks will be suppressed from the leak report.

However, presumably HideExpectedLeaksRegisteredByPointer is intended to be undefined. As to why that is so I am not sure, but I cannot imagine Pierre undefined it by accident. Anyway, perhaps it is reasonable to define HideExpectedLeaksRegisteredByPointer. You might care to experiment with that.

like image 140
David Heffernan Avatar answered Sep 20 '22 13:09

David Heffernan