用错误的“”调用构造函数;这个“;指针.这是堆栈损坏吗

Constructor called with wrong "this" pointer. Is this stack corruption?

本文关键字:堆栈 损坏 指针 这个 错误 调用 构造函数      更新时间:2023-10-16

编辑:我在评论者的帮助下想明白了。为了回答我在标题中提出的问题:不,它不是堆栈损坏,它的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) 
{ }

现在,我发现了一个内存损坏错误,并设法将其追踪到以下代码片段:

itvt itvt::get_split(bool le) const
{
   itvt result(*this);
   [...]
}

使用gdb,我发现对构造函数的调用似乎并没有构造"结果"对象:

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
}

深入观察,我发现在复制构造函数中,"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

由于"result"是在地址0x7fff5fbfdee0的堆栈上分配的,我希望复制构造函数内的"this"指针指向相同的地址。相反,它指向0x7fff5fbfdf80。

看起来复制构造函数正在初始化东西,而不是调用它的堆栈上的"result"对象。事实上,我可以很好地访问构造函数初始化的内存位置:

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
}

我的第一个问题:"this"指针指向错误对象这一事实能否被解释为正常行为?这似乎是一些奇怪的内存损坏问题,但也许我错过了什么。

我正在使用g++和"-O0-ggdb"标志进行编译,并对所有内容进行了新的重新编译。下面是我的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.

我的第二个问题:如果是内存损坏,你对我如何追踪来源有什么建议吗。我通常使用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;
  [...]
};

第二个问题:使用valgrind,这正是您真正需要的。它跟踪每一个分配/空闲,并告诉你是否尝试使用空闲内存以及许多其他东西。它会显示内存损坏。

我在评论者的帮助下发现了这一点:看起来gdb没有说实话(可能是因为旧的gdb版本)。该程序实际上按预期运行。

促使我调查这个问题的实际错误行为与对象初始化完全无关。