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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With