Debugging Memory Problems with MemoryScape™ : Chapter 1 Locating Memory Problems : Batch Scripting and Using the CLI : Using the -dheap Command : Checking for Dangling Pointers: dheap -is_dangling:
Checking for Dangling Pointers: dheap -is_dangling:
The dheap -is_dangling command lets you determine if a pointer is still pointing into a deallocated memory block.
You can also use the dheap -is_dangling command to determine if an address refers to a block that was once allocated but has not yet been recycled. That is, this command lets you know if a pointer is pointing into deallocated memory.
Here’s a small program that illustrates a dangling pointer:
main(int argc, char **argv)
{
int *addr = 0; /* Pointer to start of block. */
int *misaddr = 0; /* Pointer to interior of block. */
addr = (int *) malloc (10 * sizeof(int));
/* Point to interior of the block. */
misaddr = addr + 5;
/* addr and misaddr now dangling. */
free (addr);
printf ("addr=%lx, misaddr=%lx\n",
(long) addr, (long) misaddr);
}
If you set a breakpoint on the printf() statement and probe the addresses of addr and misaddr, the CLI displays the following:
d1.<> dheap -is_dangling 0x80496d0
process: 0x80496d0
1 (19405): dangling

d1.<> dheap -is_dangling 0x80496e4
process: 0x80496e4
1 (19405): dangling interior
This example is contrived. When creating this example, the variables were examined for their address and their addresses were used as arguments. In a realistic program, you’d find the memory block referenced by a pointer and then use that value. In this case, because it is so simple, using the CLI dprint command gives you the information you need. For example:
d1.<> dprint addr
addr = 0x080496d0 (Dangling) -> 0x00000000 (0)
d1.<> dprint misaddr
misaddr = 0x080496e4 (Dangling Interior) -> 0x00000000 (0)
If a pointer is pointing into memory that is deallocated, and this memory is being hoarded, the CLI also lets you know that you are looking at hoarded memory.