adizone
adizone

Reputation: 213

Can GDB help to find out when a memory address is freed?

Can GDB track when a memory address is freed? Putting a watch on the address doesn't help, since it does not break when memory at address is freed, but only when it is touched.

I think by using free(), only the pointer to the memory is freed, but the contents can still exist, until it is used by another memory allocation function.

(gdb) p (char *)0xee20229c
$2 = 0xee20229c "XYZ"
(gdb) watch *(char *)0xee20229c
Hardware watchpoint 3: *(char *) 3995083420
(gdb) c
Continuing.
...
...
Hardware watchpoint 3: *(char *) 3995083420

Old value = 88 'X'    // Changes only when the contents are overwritten,
New value = 0 '\0'    // but not when the memory location '0xee20229c' is freed.

Upvotes: 5

Views: 5239

Answers (2)

VectorVortec
VectorVortec

Reputation: 719

The Russian's answer is not incorrect, but you can do some things to determine whether the memory is freed in gdb, if you are willing to run the program multiple times.

Since you are using gdb, you can simply free the memory in question a second time. If the program has a segmentation error on the second free, you tried to free memory that was already freed.

In an alternate method, you can also set the memory pointer to NULL when it is freed. Then you only need to check for the NULL pointer. While this might seem cleaner, you will be erasing some info that could be useful in your hunt for memory issues.

Valgrind and "g++ -g -fsanitize=address yourProgram.cpp -o yourProgram -lasan" will also give some info which can be used to narrow the search. They are not infallible, though.

Upvotes: 0

Employed Russian
Employed Russian

Reputation: 213516

Can GDB track when a memory address is freed?

In general, no. However, if you know something about the malloc and free being used, then yes.

Putting a watch on the address doesn't help, since it does not break when memory at address is freed, but only when it is touched.

That is correct.

I think by using free(), only the pointer to the memory is freed,

A pointer can't be freed. A block of memory that the pointer points to is freed.

but the contents can still exist, until it is used by another memory allocation function.

Correct.

So, if you want to know when 0xee20229c is being freed, and you know nothing about your malloc implementation, then the only way to solve this is with conditional breakpoint on free.

but for condition I would need to specify the exact argument passed to free(), which might not be the same name across functions.

I am not sure what you mean. The condition you want is: the address being free'd is 0xee20229c

Setting this condition may require that you have debug info available for your libc, or that you specify it via registers (you'll need to know the ABI for your platform).

What if you do know something about malloc?

Many malloc implementations keep "housekeeping" info just before the heap block. If your implementation does, then setting a watchpoint on address-of-interest - 4 or - 8 may get you desired result.

Upvotes: 4

Related Questions