Delphi Memory Leak: how to find location of code line that actually causes memory leak

1.5k views Asked by At

I was debugging memory leak. I want to locate the actual code line that causes a memory leak. I searched on the internet for open-source tools and found FastMM I set up it for with FullDebugMode and LogMemoryLeakDetailToFile. I got the following log text file: A memory block has been leaked. The size is: 56

This block was allocated by thread 0x3840, and the stack trace (return addresses) at the time was:

424559 [FastMM4.pas][FastMM4][_ZN7Fastmm411DebugGetMemEx][9659]
4248F6 [FastMM4.pas][FastMM4][_ZN7Fastmm415DebugReallocMemEPvx][9878]
40938D [System.pas][System][_ZN6System11_ReallocMemERPvx][4819]
412C9C [System.pas][System][_ZN6System17DynArraySetLengthERPvS0_xPx][35000]
4623F1 [System.Generics.Collections.pas][System.Generics.Collections][_ZN6System8Generics11Collections11TListHelperE@InternalSetCapacity][3845]
460BD6 [System.Generics.Collections.pas][System.Generics.Collections][_ZN6System8Generics11Collections11TListHelperE@InternalGrow][3106]
460C0D [System.Generics.Collections.pas][System.Generics.Collections][_ZN6System8Generics11Collections11TListHelperE@InternalGrowCheck][3112]
45F243 [System.Generics.Collections.pas][System.Generics.Collections][_ZN6System8Generics11Collections11TListHelperE@InternalAdd8][2487]
6EF13D [System.JSON.pas][System.JSON][_ZN6System4Json11TJSONObjectE@AddDescendant][1831]
6EF64E [System.JSON.pas][System.JSON][_ZN6System4Json11TJSONObjectE@ParsePair][1979]
6EF4BB [System.JSON.pas][System.JSON][_ZN6System4Json11TJSONObjectE@Parse][1924]
The block is currently used for an object of class: Unknown

So it only notifies if any memory leaks occur. I don't understand how to use this file and find the actual code line that causes memory. is there any way to find that easily?

2

There are 2 answers

1
fpiette On

To find memory leaks, madExcept is really a good tool. It is very easy to use and don't require any modification in source code.

Once installed in Delphi, you have a new "madExcept settings..." menu item under "Project" which gives the following screen:

enter image description here

On that screen dump, I already checked the correct options to detect memory leaks among other things.

Just compile and run your program. When you quit it, it will generate an error report. For example, assuming you have this source code which create a memory leak on line 28:

enter image description here

The following report is generated:

enter image description here

That report show one memory leak. The leak is a TObject instance, occuring 2.47sec after program start. The call stack show that GetMem has been called without corresponding FreeMem. If you follow the call stack, you can see that TForm10.Button1Click on line 28 is the culprit in the code I wrote.

madExcept report more than memory leaks: it also report resource leaks, buffer overflow or underflow and frozen main thread.

1
Matt Davis On

I have had some success using MadExcept tools to do this, although it has also stopped me from being able to run some projects when I turn leak detection on.