Difference between revisions of "leakview"

From Lazarus wiki
Jump to navigationJump to search
(Differences between Windows and Unix explained.)
Line 8: Line 8:
 
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)''
 
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)''
  
In your .lpr, in the beginning of your code, add
+
On Windows, Lazarus will display a dialog box at program termination that displays essential information on allocation and freeing of memory blocks.
 +
 
 +
On Unix (including Linux and Mac OS X) this is not possible. It is possible, however, to log the output of the heaptrc unit to a file. In order to accomplish this, add in your .lpr, in the beginning of your code, the following code fragments
 
<syntaxhighlight>
 
<syntaxhighlight>
 
   {$DEFINE debug}     
 
   {$DEFINE debug}     
Line 34: Line 36:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
to redirect the heaptrc output to file.
+
to redirect the heaptrc output to file. This code also works on Windows, if you prefer that the results are logged to a file.
  
 
=== Enabling heaptrc in FPC ===
 
=== Enabling heaptrc in FPC ===

Revision as of 19:05, 21 April 2014

Overview

Leakview allows fast navigation trough HeapTrc (and other?) leaks.

Usage

Leakview reads heaptrc output. For this to work, you'll need to enable heaptrc in your code:

Enabling 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)

On Windows, Lazarus will display a dialog box at program termination that displays essential information on allocation and freeing of memory blocks.

On Unix (including Linux and Mac OS X) this is not possible. It is possible, however, to log the output of the heaptrc unit to a file. In order to accomplish this, add in your .lpr, in the beginning of your code, the following code fragments

  {$DEFINE debug}     

uses
  ...
  {$IFDEF debug}
  , SysUtils
  {$ENDIF}

...

begin
  {$IFDEF DEBUG} 
  // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh
  // This avoids interference when running a production/default build without -gh

  // Set up -gh output for the Leakview package:
  if FileExists('heap.trc') then
    DeleteFile('heap.trc');
  SetHeapTraceOutput('heap.trc');
  {$ENDIF DEBUG}

  ...
end.

to redirect the heaptrc output to file. This code also works on Windows, if you prefer that the results are logged to a file.

Enabling heaptrc in FPC

In FPC, you can specify -gh in your compilation option or include the heaptrc unit in your uses clause.

Then redirect the heaptrc output to file (instead of standard output). You can use similar code to the Lazarus code or alternatively, set the environment variable, e.g. on *nix:

export HEAPTRC="log=heap.trc"

or Windows:

set HEAPTRC="log=heap.trc"