调试堆栈值损坏的好方法是什么。在我的程序中,有时 this 指针的地址会在一个方法返回后更改,该方法对文件描述符执行关闭操作。我调试了几个小时的程序,但找不到问题所在。
找出 this 指针地址发生变化的好方法是什么?当我在 this 指针上手动添加 watch 时,不会发生错误。当我尽可能地剥离我的代码时,错误仍然发生。我尝试了 Valgrind,但没有发现任何早期堆栈损坏。
我设法检测到错误发生的时间,我在 64 位模式下编译了代码。它的地址从 0xxxxxxx 更改为 0x1000000xxxxxxx。我在发生错误的方法中检查了它的地址,我在地址更改时发现了这一点(请参阅第一个段落)。
有没有其他方法可以找出问题的原因?
最佳答案
您可能想试一试 address-sanitizer .是available in gcc 4.8 :
AddressSanitizer , a fast memory error detector, has been added and can be enabled via
-fsanitize=address
. Memory access instructions will be instrumented to detect heap-, stack-, and global-buffer overflow as well as use-after-free bugs. To get nicer stacktraces, use-fno-omit-frame-pointer
. The AddressSanitizer is available on IA-32/x86-64/x32/PowerPC/PowerPC64 GNU/Linux and on x86-64 Darwin.
在 GCC 中(但显然不是 clang),你需要在 both 编译器标志 和 链接器标志中指定 -fsanitize=address
,如 this related answer 中所述.
关于c++ - 调试堆栈值损坏的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398914/