Difference between revisions of "leakview"

From Lazarus wiki
Jump to navigationJump to search
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
'''Leakview''' ('[View|Leaks and Traces]') allows fast navigation trough [[heaptrc|HeapTrc]] leak reports.
+
{{leakview}}
 +
 
 +
'''Leakview''' allows fast navigation through [[heaptrc|HeapTrc]] leak reports.
  
 
[[image:leakview.png]]
 
[[image:leakview.png]]
  
 
== Usage ==
 
== Usage ==
Leakview reads [[heaptrc]] output. For this to work, you'll need to enable heaptrc in your code:
+
The Leakview utility is available in the IDE under ''View / Leaks and Traces''. 
 +
 
 +
Leakview reads [[heaptrc]] output. For this to work, you'll need to enable heaptrc in your code:<br>
 +
'''Never include heaptrc in your uses clause manually. This is done implicitly by the compiler when -gh is specified'''<br>
  
 
=== Enabling heaptrc in Lazarus ===
 
=== 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)''
+
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)'' It is also a good idea to include line numbers, so -glh or -gh -gl
 +
 
 +
To get meaningful heaptrc results with descriptions to your code lines and not just assembler addresses, go to Tools/Options/Debugger/general and set a debugger.
  
 
You can then let the program log the output of the heaptrc unit to a file. Add the following code fragments in your .lpr, at the beginning of your code to redirect the heaptrc output to file:
 
You can then let the program log the output of the heaptrc unit to a file. Add the following code fragments in your .lpr, at the beginning of your code to redirect the heaptrc output to file:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
   {$DEFINE debug}    // do this here or you can define a -dDEBUG in Project Options/Other/Custom Options, i.e. in a build mode so you can set up a Debug with leakview and a Default build mode without it
 
   {$DEFINE debug}    // do this here or you can define a -dDEBUG in Project Options/Other/Custom Options, i.e. in a build mode so you can set up a Debug with leakview and a Default build mode without it
  
Line 35: Line 42:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=== Irrelevant dialog box section ===
 
{{Note|You should not see the dialog boxes below if you have properly configured heaptrc like in the example above.}}
 
On Unix systems the leakview results are stored in a file the path of which is specified. Windows displays dialog boxes like the following example:
 
 
 
[[File:Win heaptrc output no error.png|Standard output of heaptrc on Windows (despite of the title there is no error in the application)]]
 
 
Alternatively, for any non-trivial leak you may see a seemingly endless series of dialog boxes like those below - which makes debugging almost impossible.
 
 
{{Warning|Why does this section on dialog boxes keep coming back? Hopefully the info below demonstrates that not configuring heaptrc on Windows is not a smart thing to do. If wiki editors disagree, please clean up article as you see fit.}}
 
 
[[File:leakdialog1.png|Standard output of heaptrc on Windows (truncated at 5 dialog boxes as you probably get the point by then]]
 
 
[[File:leakdialog2.png|Standard output of heaptrc on Windows (truncated at 5 dialog boxes as you probably get the point by then]]
 
 
[[File:leakdialog3.png|Standard output of heaptrc on Windows (truncated at 5 dialog boxes as you probably get the point by then]]
 
 
[[File:leakdialog4.png|Standard output of heaptrc on Windows (truncated at 5 dialog boxes as you probably get the point by then]]
 
 
[[File:leakdialog5.png|Standard output of heaptrc on Windows (truncated at 5 dialog boxes as you probably get the point by then]]
 
  
 
=== Enabling heaptrc in FPC ===
 
=== Enabling heaptrc in FPC ===
In FPC, you can specify -gh in your compiler options. Never include the heaptrc unit in your uses clause manually.
+
In FPC, you can specify -gh in your compiler options in fpc.cfg or on the commandline.<br>
 
You can test if your binary is compiled with heaptrace by:
 
You can test if your binary is compiled with heaptrace by:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
{$if Declared(UseHeapTrace)}...{$ifend}
 
{$if Declared(UseHeapTrace)}...{$ifend}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 72: Line 58:
 
set HEAPTRC="log=heap.trc"
 
set HEAPTRC="log=heap.trc"
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[Category:Lazarus]]
 
[[Category:Debugging]]
 
[[Category:Lazarus IDE plugins]]
 

Latest revision as of 16:05, 27 January 2022

English (en) русский (ru)

Leakview allows fast navigation through HeapTrc leak reports.

leakview.png

Usage

The Leakview utility is available in the IDE under View / Leaks and Traces.

Leakview reads heaptrc output. For this to work, you'll need to enable heaptrc in your code:
Never include heaptrc in your uses clause manually. This is done implicitly by the compiler when -gh is specified

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) It is also a good idea to include line numbers, so -glh or -gh -gl

To get meaningful heaptrc results with descriptions to your code lines and not just assembler addresses, go to Tools/Options/Debugger/general and set a debugger.

You can then let the program log the output of the heaptrc unit to a file. Add the following code fragments in your .lpr, at the beginning of your code to redirect the heaptrc output to file:

  {$DEFINE debug}     // do this here or you can define a -dDEBUG in Project Options/Other/Custom Options, i.e. in a build mode so you can set up a Debug with leakview and a Default build mode without it

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.

Enabling heaptrc in FPC

In FPC, you can specify -gh in your compiler options in fpc.cfg or on the commandline.
You can test if your binary is compiled with heaptrace by:

{$if Declared(UseHeapTrace)}...{$ifend}

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"