leo*_*leo 6 c++ gdb g++ corruption
编辑:
我在评论者的帮助下弄清楚了.要回答我标题中提出的问题:不,它不是堆栈损坏,它的gdb报告错误的值.程序实际上按预期运行并具有正确的this指针.促使我发布这个问题的实际错误行为可能与我在这里描述的问题完全无关.
首先警告.我相信这是一个内存损坏问题,我通常不会期待一个答案,除了"彻底检查你的代码",但我已经看到这种行为反复出现,并希望你们中的一些人对这个问题有所见解,以及我是怎么做的可以找到它的来源.
我目前正在实施一个区间静态分析,它跟踪C程序中可能的变量范围.我的基本区间类的复制构造函数如下所示:
itvt::itvt(const itvt& i)
: _i(i.type == INTBV ? new intbv_intervalt(i.i()) : NULL),
_f(i.type == FLOAT ? new float_intervalt(i.f()) : NULL),
type(i.type), other_bottom(i.other_bottom)
{ }
Run Code Online (Sandbox Code Playgroud)
现在,我发现了一个内存损坏错误并设法将其跟踪到以下代码片段:
itvt itvt::get_split(bool le) const
{
itvt result(*this);
[...]
}
Run Code Online (Sandbox Code Playgroud)
使用gdb,我发现对构造函数的调用似乎没有构造"result"对象:
Breakpoint 1, itvt::get_split (this=0x1016af560, le=false) at itv.cpp:517
517 itvt result(*this);
(gdb) n
519 if(is_singleton() || is_bot())
(gdb) print result
$3 = {
_i = {
_M_ptr = 0x7fff5fbfe100
},
_f = {
_M_ptr = 0x7fff5fbfed60
},
type = 1606410016,
other_bottom = 255
}
(gdb) print *this
$4 = {
_i = {
_M_ptr = 0x1020833a0
},
_f = {
_M_ptr = 0x0
},
type = itvt::INTBV,
other_bottom = false
}
Run Code Online (Sandbox Code Playgroud)
仔细观察,我发现在复制构造函数中,"this"指针指向错误的对象:
Breakpoint 1, itvt::get_split (this=0x1016af560, le=false) at itv.cpp:517
517 itvt result(*this);
(gdb) print &result
$5 = (itvt *) 0x7fff5fbfdee0
(gdb) s
itvt::itvt (this=0x7fff5fbfdf80, i=@0x1016af560) at itv.cpp:500
500 type(i.type), other_bottom(i.other_bottom)
(gdb) print this
$6 = (itvt * const) 0x7fff5fbfdf80
Run Code Online (Sandbox Code Playgroud)
由于在地址0x7fff5fbfdee0的堆栈上分配了"result",我希望复制构造函数中的"this"指针指向同一个地址.相反,它指向0x7fff5fbfdf80.
看起来复制构造函数正在初始化某些东西,但不是调用它的堆栈上的"结果"对象.实际上,我可以访问构造函数初始化的内存位置:
Breakpoint 1, itvt::get_split (this=0x1016af560, le=false) at itv.cpp:517
517 itvt result(*this);
(gdb) s
itvt::itvt (this=0x7fff5fbfdf80, i=@0x1016af560) at itv.cpp:500
500 type(i.type), other_bottom(i.other_bottom)
(gdb) finish
Run till exit from #0 itvt::itvt (this=0x7fff5fbfdf80, i=@0x1016af560) at itv.cpp:500
itvt::get_split (this=0x1016af560, le=false) at itv.cpp:519
519 if(is_singleton() || is_bot())
(gdb) print *((const itvt*) (0x7fff5fbfdf80))
$7 = {
_i = {
_M_ptr = 0x1016b6d10
},
_f = {
_M_ptr = 0x0
},
type = itvt::INTBV,
other_bottom = false
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题: "this"指针指向错误对象的事实可以解释为正常行为吗?这似乎是一些奇怪的内存损坏问题,但也许我错过了一些东西.
我正在使用g ++和"-O0 -ggdb"标志进行编译,并对btw的所有内容进行了重新编译.这是我的g ++版本:
leo@scythe ai$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
我的第二个问题:如果是内存损坏,你对我如何追踪源代码有什么建议吗?我通常使用gdb将这些问题跟到他们的根本原因,但我不知道现在在哪里看.
这不是我第一次遇到这种特定的行为.我看到它发生在调查其他人的错误时.我从来没有直接设法直接解决它,它只是停止发生或至少是一些其他代码更改后的可见问题.这让我相信它可能只是一个使用gdb查看堆栈的奇怪艺术品.
我感谢您提供的任何建议或见解.
编辑: 以下是itvt类的相关片段:
class itvt
{
protected:
typedef std::auto_ptr<intbv_intervalt> iptrt;
typedef std::auto_ptr<float_intervalt> fptrt;
iptrt _i;
fptrt _f;
public:
typedef enum {INTBV, FLOAT, OTHER} itv_typet;
itv_typet type;
bool other_bottom;
//copy constr
itvt(const itvt& i);
inline intbv_intervalt& i() { return *_i; }
inline float_intervalt& f() { return *_f; }
inline const intbv_intervalt& i() const { return *_i; }
inline const float_intervalt& f() const { return *_f; }
itvt get_split(bool le) const;
[...]
};
Run Code Online (Sandbox Code Playgroud)