heaptrc is a unit that can be used to debug allocation and deallocation of memory blocks. It keeps track of calls to getmem/freemem, and, implicitly, of New/Dispose statements.
When a program using heaptrc ends, heaptrc can write out the total memory used and a list of allocated but not freed blocks to a file. When run in the console (*nix or Windows), heaptrc will print this output to screen unless otherwise instructed. In Lazarus GUI programs on Windows, the output will appear in numerous small dialog boxes, which may be very unpractical.
On *nix (including BSD, Linux and Mac OS X), by default, nothing will be shown for GUI programs. See leakview for details on how to make use of heaptrc effectively.
Heaptrc can be added to a project in two ways in FPC.
1) Explicitly add the unit to your uses section. But read warning below.
program Helloworld; uses heaptrc, cthreads, Classes, SysUtils begin writeln('hello world'); end.
2) Add a parameter -gh to your compilation command line
fpc -gh helloworld.pas
This will add heaptrc implicitly as the first unit of uses clauss.
Using heaptrc in Lazarus
To enable this in your Lazarus project: go to Project Options/Linking and in the Debugging section enable Use Heaptrc unit (check for mem-leaks) (-gh)
Show report only on leak
If you want to show heaptrc report dialog only if there were leaks in your application, then put this command somewhere in your main project source file:
GlobalSkipIfNoLeaks := true;
Programatically disable report
If you want to disable report from your code then you can directly manipulate UseHeapTrace variable at program startup
UseHeapTrace := false;
Continue execution even on heap error
If you want you application to continue execution even in case of a heap error then manipulate HaltOnError variable at program startup
HaltOnError := false;
Redirect report to file
If you want to redirect leak report to file then add this to beginning of you main project file:
Don't put heaptrc into uses section more than one time. It's preferred if you never put into a unit uses section, but only into program uses section.
I would like to share my example of wrong heaptrc usage... After some hard debugging and troubleshooting I wanted to dig a little into heaptrc unit to see if there was anything more inside of it which I could have used along with TLogger. Therefore besides being the first unit in project, I have included it in one of my other units where I temporarily needed it. What a wrong move!!! >:( :'( %) In meantime I have solved my problem but completely forgotten to remove heaptrc from the uses in that other unit. Well, many, many man hours later I have figured out why my application had sudden leaks, hangs and freezes, and totally different errors with heaptrc used or not with a project. What a relief! I wish IDE had at least warned me that I had heaptrc which was not the first used unit in a project, or that I had 2 such units. Doc says only that it should be the first used unit in a project, but nothing said that it was dangerous to include it once more. Cheers!