DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More Memory Leaks - fs(3C++)

Looking at the freestore

At any given moment during the execution of a C++ program, the freestore contains all those objects which were newed at some time since the start of the program, but have not yet been deleted. (This includes any objects newed during static initialization.) For each of these objects in the freestore, fs keeps track of the following information:

fs keeps track of objects created by regular new, array new, nothrow new, and placement new - in other words, all of the variations of new in Standard C++. We also need ``names'' or ``handles'' by which we can refer to the objects in the freestore. These handles will come in useful when we make requests of fs. Notice that an object's memory address is a poor handle by which to refer to it, since (1) addresses are tedious to type, and (2) different objects over the course of program execution can have the same address. Our solution is to give each freestore object a small integer handle. The first newed object is given the handle 0, and successively newed objects are given handles continuing up the integers. Thus, an object's handle distinguishes it from all other objects that are, were, or ever will be in the freestore.

All the above symbolic information can be displayed by calling the library function fs_showall() somewhere in the current context -- that is, either from within the debugger, or from within the program itself. The information is printed on the standard error output stderr. Here is an example output:

       #0: int, newed on line `t1.c`5
         (size 4 at 0x24b84)
       #2: int[3], newed on line `t2.c`32
         (size 3 x 4 at 0x24bd8)
       #5: T, newed on line `t1.c`14
         (size 32 at 0x24c34)
       Total of 7 objects newed since start,
         3 still in freestore.

This is interpreted as follows:

In a large program, there will often be many objects in the freestore. To aid in digesting the volume of information that would be produced by a call to fs_showall(), the user may set ``marks.'' A call to the function fs_mark() lays down a ``mark'' at the current moment in time (that is, not at the current source code location). Once a mark has been laid down, a call to fs_since() will display the information for only those objects which were newed since the mark. To delete the mark, the user calls fs_unmark(). The programmer may lay down as many marks as desired; fs_since() will always display the information for only those objects newed since the latest mark, and fs_unmark() will only delete the latest mark. The user can see the current set of marks by calling the function fs_status(). The output of this command will look something like the following:
       Marks after: 3, 12, 62

This means that there are currently three marks: the first mark was laid down sometime after object #3 was newed, but before object #4 was newed; the second mark was laid down after object #12 but before object #13; and the third mark after object #62 but before #63. Calling fs_since() with this setting of marks will result in the information for objects #63, #64, ..., being displayed.

If the user is interested in seeing all the objects newed after a particular object #n (regardless of whether a mark was laid down after #n), then a call to fs_sincen(n) does the trick.


Next topic: Freestore events
Previous topic: fs to the Rescue

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004