Difference between revisions of "heaptrc"

From Lazarus wiki
Jump to navigationJump to search
(added GlobalSkipIfNoLeaks info)
m (rearranged GlobalSkipIfNoLeaks info)
Line 32: Line 32:
 
This will add heaptrc implicitly as the first unit of uses clauss.
 
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)''
 +
 +
=== 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:
 
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:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
   GlobalSkipIfNoLeaks := true;
 
   GlobalSkipIfNoLeaks := true;
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=== 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 10:21, 20 May 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.


Standard output of heaptrc on Windows (despite of the title there is no error in the application)


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)

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;

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.


by avra on Lazarus forum

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