The objective of the project was to implement a tool that allows analyzing the
heaps of any process, i.e. getting the listed heaps and allocated memory
The developed utility is used as an auxiliary tool for search and elimination
of memory leaks, as well as for memory management optimization. The utility
generates a complete description of all the heaps of a certain process. By
comparing several descriptions obtained at checkpoints, it is possible to
detect a memory leak, estimate its size, and detect the addresses of the "lost"
blocks as well. Knowing the addresses of the "lost" blocks makes it possible to
look through their dump and try to detect the leakage source code by means of a
debugger (in case the blocks contain text data, or the objects created in the
heap are provided with special markers, the task is quite solvable).
The utility targets the MS Windows 2k platform and later. It uses the standard
API to analyze the heaps of a process (Tool Help API) and access the heaps of
any process as well. The direct use of the Tool Help API for listing the heaps
turned out to be impossible. When address to the non-current process, it
continuously allocates memory and heavily loads the processor. However, this
feature does not appear when working with the current process,
that's why the following trick was implemented in our utility. A small chunk of
memory is allocated in the context of a non-current target process, and the
code is written to this memory that will initialize the heaps scanning. Then a
remote thread is created in the target process which execution starts with the
code generated by Heap Walker. Thus, the heaps scanning runs in the context of
the target process, and the above error does not occur.