Skip to content

Usage

HeapTrc Usage

You must use the -gh switch, to let the compiler insert the unit by itself, so you don't have to include it in your uses clause. In fact, as of version 3.0.0 you may no longer do so.

The below example shows how to use the heaptrc unit.

This is the memory dump shown when running this program in a standard way:

Marked memory at 0040FA50 invalid
Wrong size : 128 allocated 64 freed
  0x00408708
  0x0040CB49
  0x0040C481
Call trace for block 0x0040FA50 size 128
  0x0040CB3D
  0x0040C481

If you use the lineinfo unit (or use the -gl switch) as well, then heaptrc will also give you the filenames and line-numbers of the procedures in the backtrace:

Marked memory at 00410DA0 invalid
Wrong size : 128 allocated 64 freed
  0x004094B8
  0x0040D8F9  main,  line 25 of heapex.pp
  0x0040D231
Call trace for block 0x00410DA0 size 128
  0x0040D8ED  main,  line 23 of heapex.pp
  0x0040D231

If lines without filename/line-number occur, this means there is a unit which has no debug info included.

Example

Program heapex;
{ 
  Program used to demonstrate the usage of heaptrc unit 
  Compile this program with the -gh command-line option
}
Var P1 : ^Longint;
    P2 : Pointer;
    I : longint;
begin
  {$IF NOT DECLARED(heaptrc)}
  {$ERROR You mmust compile this program with -gh}
  {$ENDIF}
  New(P1);
  // causes previous allocation not to be de-allocated
  New(P1);
  Dispose(P1);
  For I:=1 to 10 do
    begin
    GetMem (P2,128);
    // When I is even, deallocate block. We loose 5 times 128
    // bytes this way.
    If (I mod 2) = 0 Then FreeMem(P2,128);
    end;
  GetMem(P2,128);
  // This will provoke an error and a memory dump
  Freemem (P2,64);
end.