2012-10-10 56 views
2

這是我的設置(煮沸)。我有一個「佈局功能」:在拋出'std :: bad_alloc'的實例後終止調用

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,它們使用稍微不同的數字來「咖喱」佈局功能。我不打印它們以節省空間(它們具有相同的結構,只有不同的數字)。最後,我使用類似於:

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

它將根據模板類型順序構建「curried」佈局函數。

現在,可能這個簡單的(煮沸)示例工作。但是,在我的真實應用程序運行時,我在LayoutFunc的拷貝構造函數中獲得了一個terminate called after throwing an instance of 'std::bad_alloc'

我認爲在設置中存在一個缺陷,與臨時引用有關,並且在消費者(在這種情況下爲LayoutFunc的拷貝構造函數)使用它之前銷燬此臨時對象。這將解釋lim(func.lim),val(func.val)失敗。但我無法看到缺陷的位置,特別是因爲curry返回了真實的lvalue。此外,我用移動構造函數嘗試它,並在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 

啊:183是A拷貝構造函數:

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

'bad_alloc'異常通常是一個標誌,表示分配提交。你有沒有檢查你的空閒內存? –

+1

你可以發佈什麼是PScalarJIT? –

+0

我不認爲問題是缺乏可用內存。在BT中,你可以看到'std :: vector '分配失敗。這可能是因爲它想要做一些不正確的事情。 – ritter

回答

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

應該

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

BЈовић評論指出我在方向找這個bug。如果您發佈了答案,則+1BЈовић。也爲了讓sehe +1而理解BT

相關問題