leakview

From Lazarus wiki
Revision as of 19:05, 21 April 2014 by Jwdietrich (talk | contribs) (Differences between Windows and Unix explained.)
Jump to navigationJump to search

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"