GDB 可以帮助找出内存地址何时被释放吗?

标签 c gdb

GDB 可以跟踪内存地址何时被释放?把 watch在地址上没有帮助,因为本地址处的内存被释放时它不会中断,但只有在它被触摸时才会中断。

我认为通过使用 free() ,只有指向内存的指针被释放,但内容仍然可以存在,直到它被另一个内存分配函数使用。

(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?



一般来说,没有。但是,如果您对 malloc 有所了解,和 free被使用,那么是的。

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,



无法释放指针。指针指向的一 block 内存被释放。

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



正确的。

所以,如果你想知道0xee20229c正在被释放,而您对 malloc 实现一无所知,那么解决此问题的唯一方法是在 free 上使用条件断点.

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



我不确定你的意思。你想要的条件是:被释放的地址是0xee20229c
设置此条件可能需要您有可用于 libc 的调试信息。 ,或者您通过寄存器指定它(您需要知道您平台的 ABI)。

如果您确实了解 malloc 怎么办? ?

许多 malloc 实现在堆 block 之前保留“管家”信息。如果你的实现是这样,那么在 address-of-interest - 4 上设置一个观察点或 - 8可能会得到你想要的结果。

关于GDB 可以帮助找出内存地址何时被释放吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506577/

相关文章:

c++ - gdb 自动进入内联函数

c - GDB:如果变量值相等则中断

c - 尝试更好地理解指针

c - 是否有一个程序(或 sed 脚本)可以从 C 文件中删除一个函数?

c - 在 WinAPI 中优雅地关闭无窗口应用程序

python - 构建 CPython 时禁用编译器优化

c - gdb:如何丢弃行溢出(而不是换行)?

c++ - qtcreator + gdb,扩展宏定义

c - 第三个函数不会运行 scanf

c - 移动库和 header