Difference between revisions of "heaptrc"
m (→Warning) |
m (→Warning) |
||
Line 10: | Line 10: | ||
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. | 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. | ||
+ | |||
+ | == Usage == | ||
+ | Heaptrc can be added to a project in two ways in FPC. | ||
+ | |||
+ | 1) Explicitly add the unit to your uses section. But read [[heaptrc#Warning|warning]] below. | ||
+ | <syntaxhighlight> | ||
+ | program Helloworld; | ||
+ | |||
+ | uses | ||
+ | heaptrc, cthreads, Classes, SysUtils | ||
+ | |||
+ | begin | ||
+ | writeln('hello world'); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | == 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)'' | ||
== Warning == | == Warning == |
Revision as of 15:59, 10 February 2016
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.
Usage
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.
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)
Warning
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!
See also
- leakview: Examples how to enable heaptrc in Lazarus and Free Pascal.
- RTL Reference for unit 'heaptrc'