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:
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.