10.5 Detecting heap memory leaks

Free Pascal has a built in mechanism to detect memory leaks. There is a plug-in unit for the memory manager that analyses the memory allocation/deallocation and prints a memory usage report after the program exits.

The unit that does this is called heaptrc. If you want to use it, you should include it as the first unit in your uses clause. Alternatively, you can supply the -gh switch to the compiler, and it will include the unit automatically for you.

After the program exits, you will get a report looking like this:

Marked memory at 0040FA50 invalid  
Wrong size : 128 allocated 64 freed  
  0x00408708  
  0x0040CB49  
  0x0040C481  
Call trace for block 0x0040FA50 size 128  
  0x0040CB3D  
  0x0040C481

The output of the heaptrc unit is customizable by setting some variables. Output can also be customized using environment variables.

You can find more information about the usage of the heaptrc unit in the Unit reference.