c++ - 调试堆栈值损坏的好方法

标签 c++ linux debugging gcc stack-corruption

调试堆栈值损坏的好方法是什么。在我的程序中,有时 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/

相关文章:

linux - 如何使用 sed 替换包含斜杠的字符串?

debugging - LiteIDE 调试消息没有指定可执行文件使用目标 exec

c++ - C++ 中奇怪的 null ref

linux - Yocto - Linux 图像生成 - 删除 qemu 支持

ruby-on-rails - 我显然遗漏了一些东西...... 'ruby' 不起作用但 './ruby' 起作用(centos 5.5,从源代码安装)

debugging - 如何使用windbg查看c#变量

debugging - 如何在 Mathematica 的模块中获取调用函数的名称?

c++ - Eigen::Map 默认模板参数如何工作?

c++ - std::string 到 LPOLESTR

c++ - 随机数函数 (C++11)