Behind the Scenes
MemoryScape intercepts calls made by your program to heap library functions that allocate and deallocate memory by using the malloc() and free() functions and related functions such as calloc() and realloc(). The technique it uses is called interposition. MemoryScape’s interposition technology uses an agent routine to intercept calls to functions in this library. This agent routine is sometimes called the Heap Interposition Agent (HIA).
You can use MemoryScape with any allocation and deallocation library that uses such functions as malloc() and free(). Typically, this library is called the malloc library. For example, the C++ new operator is almost always built on top of the malloc() function. If it is, MemoryScape can track it. Similarly, if your Fortran implementation use malloc() and free() functions to manage memory, MemoryScape can track Fortran heap memory use.
You can interpose the agent in two ways:
• You can tell MemoryScape to preload the agent. Preloading means that the loader places an object before the object listed in the application’s loader table.
When a routine references a symbol in another routine, the linker searches for that symbol’s definition. Because the agent’s routine is the first object in the table, your program invokes the agent’s routine instead of the routine that was initially linked in.
If your program attaches to an already running program, you must explicitly link this other program with the agent.
After the agent intercepts a call, it calls the original function. This means that you can use MemoryScape with most memory allocators.
Figure 5 shows how the agent interacts with your program and the heap library.
Because MemoryScape uses interposition, memory debugging can be considered non-invasive. That is, MemoryScape doesn’t rewrite or augment your program’s code, and you don’t have to do anything in your program. Because the agent lives in the user space, it will add a small amount of overhead to the program’s behavior, but it should not be significant.