Next: , Previous: GPCUtil, Up: GPC Units

6.15.7 Primitive heap checking

The following listing contains the interface of the HeapMon unit.

This unit provide a rather primitive means to watch the heap, i.e. check if all pointers that were allocated are released again. This is meant as a debugging help for avoiding memory leaks.

More extensive heap checking is provided by libraries like efence which can be used in GPC programs without special provisions.

     { A unit to watch the heap, i.e. check if all pointers that were
       allocated are released again. It is meant as a debugging help to
       detect memory leaks.
       Use it in the main program before all other units. When, at the
       end of the program, some pointers that were allocated, have not
       been released, the unit writes a report to StdErr or another file
       (see below). Only pointers allocated via the Pascal mechanisms
       (New, GetMem) are tracked, not pointers allocated with direct libc
       calls or from C code. After a runtime error, pointers are not
       Note that many units and libraries allocate memory for their own
       purposes and don't always release it at the end. Therefore, the
       usefulness of this unit is rather limited.
       Copyright (C) 1998-2005 Free Software Foundation, Inc.
       Author: Frank Heckenbach <>
       This file is part of GNU Pascal.
       GNU Pascal is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
       by the Free Software Foundation; either version 2, or (at your
       option) any later version.
       GNU Pascal is distributed in the hope that it will be useful, but
       WITHOUT ANY WARRANTY; without even the implied warranty of
       General Public License for more details.
       You should have received a copy of the GNU General Public License
       along with GNU Pascal; see the file COPYING. If not, write to the
       Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
       02111-1307, USA.
       As a special exception, if you link this file with files compiled
       with a GNU compiler to produce an executable, this does not cause
       the resulting executable to be covered by the GNU General Public
       License. This exception does not however invalidate any other
       reasons why the executable file might be covered by the GNU
       General Public License. }
     {$if __GPC_RELEASE__ < 20030303}
     {$error This unit requires GPC release 20030303 or newer.}
     unit HeapMon;
     uses GPC;
     { This unit is automatically activated when used. The following
       declarations are only needed for special purposes. }
     { The report generated at the end can be redirected to a certain
       file by pointing HeapMonOutput to it. If not set, the report will
       be printed to the error messages file given with --gpc-rts
       options if given, and StdErr otherwise. }
       HeapMonOutput: ^Text = nil;
     { HeapMonReport can be used to print a report on non-released memory
       blocks at an arbitrary point during a program run to the file f.
       It is invoked automatically at the end, so usually you don't have
       to call it. Returns True if any non-released blocks were found,
       False otherwise. }
     function HeapMonReport (var f: Text; DoRestoreTerminal: Boolean) =
       Res: Boolean; attribute (ignorable);