c++ - 抛出 'std::bad_alloc' 实例后调用终止

标签 c++ c++11

这是我的设置(归结为)。我有一个“布局功能”:

  struct LayoutFunc {
    LayoutFunc( int limit , int value ) { lim.push_back(limit); val.push_back(value); }
    //LayoutFunc(LayoutFunc&& func) : lim(func.lim),val(func.val) {}
    LayoutFunc(const LayoutFunc& func) : lim(func.lim),val(func.val) {} // error: std::bad_alloc
    LayoutFunc(const std::vector<int>& lim_, 
               const std::vector<int>& val_ ) : lim(lim_),val(val_) {}
    LayoutFunc curry( int limit , int value ) const {
      std::vector<int> rlim(lim);
      std::vector<int> rval(val);
      rlim.push_back(limit);
      rval.push_back(value);
      LayoutFunc ret(rlim,rval);
      return ret;
    };
    std::vector<int> lim;
    std::vector<int> val;
  };

然后我有一个使用 LayoutFunc 的类:

template<class T> class A
{
public:
  A( const LayoutFunc& lf_ ) : lf(lf_), member( lf.curry(1,0) ) {}
  A(const A& a): lf(lf), member(a.function) {}  // corresponds to line 183 in real code
private:
  LayoutFunc lf;
  T member;
};

数据成员的顺序是正确的。还有更多类型,如 class A它使用稍微不同的数字来“ curry ”布局函数。为了节省空间,我不在这里打印它们(它们具有相同的结构,只是数字不同)。最后我使用类似的东西:

A< B< C<int> > > a( LayoutFunc(1,0) );

它将根据模板类型顺序构建“柯里化(Currying)”布局函数。

现在,这个简单(简化)的示例可能有效。但是,在我的实际应用程序运行时,我得到 terminate called after throwing an instance of 'std::bad_alloc'LayoutFunc 的复制构造函数中.

我认为设置中存在一个缺陷,该缺陷与引用临时对象有关,并且该临时对象在使用者(在本例中为 LayoutFunc 的复制构造函数)使用它之前被销毁。这可以解释lim(func.lim),val(func.val)失败。但我看不出缺陷在哪里,特别是因为 curry返回 true lvalue 。我还尝试使用 move 构造函数并在 c++11 模式下编译。相同的行为。

这里是回溯:

#0  0x00007ffff6437445 in __GI_raise (sig=<optimised out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x00007ffff643abab in __GI_abort () at abort.c:91
#2  0x00007ffff6caa69d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff6ca8846 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff6ca8873 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff6ca896e in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff6c556a2 in std::__throw_bad_alloc() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00000000004089f6 in allocate (__n=18446744073709551592, this=0x7fffffffdaf8) at /usr/include/c++/4.6/ext/new_allocator.h:90
#8  _M_allocate (__n=18446744073709551592, this=0x7fffffffdaf8) at /usr/include/c++/4.6/bits/stl_vector.h:150
#9  _Vector_base (__a=..., __n=18446744073709551592, this=0x7fffffffdaf8) at /usr/include/c++/4.6/bits/stl_vector.h:123
#10 vector (__x=..., this=0x7fffffffdaf8) at /usr/include/c++/4.6/bits/stl_vector.h:279
#11 LayoutFunc (func=..., this=0x7fffffffdae0) at layoutfunc.h:17
#12 A (a=..., this=0x7fffffffdad0) at A.h:183

A.h:183 是 A 的复制构造函数:

  A(const A& a): lf(lf), member(a.function) {}

最佳答案

A(const A& a): lf(lf), member(a.function) {}

应该是

A(const A& a): lf(a.lf), member(a.function) {}

BЈовић 评论为我指明了查找此错误的方向。如果您发布答案,+1 BЈовић。另外+1让sehe理解BT

关于c++ - 抛出 'std::bad_alloc' 实例后调用终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12817670/

相关文章:

c++ - 类继承 : Constructor and member functions of class not recognized by compiler

c++ - 在类中存储单例指针

c++ - SDL、OpenGL : Segmentation fault

c++ - 行尾字数统计 (C++)

C++ 将 unique_ptr 移动到 vector 并继续使用它

c++ - 有没有办法让 operator= 用于枚举?

C++ - 重复使用 istringstream

c++ - 我应该如何更正此 priority_queue 比较功能?

c++ - 将单个成员从 vector A 分配给 vector B 的智能方法

c++ - 纯虚最终函数 : legal in C++11