插入std :: unordered_map时std :: bad_alloc?

时间:2015-03-16 20:04:10

标签: c++ debugging memory dictionary bad-alloc

我有时会为以下代码获取std :: bad_alloc:

typedef std::unordered_map<chash, block_extended_info> map_type;
map_type m_foo; 

// the transgressor:
auto r = m_foo.insert(map_type::value_type(id, bei));

在运行使用包含该行的方法的测试用例时,有时会发生这种情况,有时则不会。我已经将代码更改为使用m_foo[id] = bei,这导致它在一次编译时停止发生,但是当我再次重新编译它失败时,所以我将其更改为上面的内容,后者继续失败。显然问题比这更深刻。

我很确定这不是内存不足的问题,因为我已经top运行,而测试用例正在运行,而且它几乎无法填满内存。

可能导致std :: bad_alloc的原因是什么? chashblock_extended_info的详细信息可能会导致此问题?这些类型的对象会被复制并传递到代码的其他部分,并且不会导致任何问题。

以下是chash

的定义
class chash {
  char data[32];
};

以下是定义block_extended_info所需的一切:

class csignature {
  chash c, r;
};
enum foob { /* ... */ };
class ct {
  uint64_t a, b;
  foob c;
};
typedef boost::variant< /* structs with ints, maps, vectors of ints */ > txin_v;
typedef boost::variant< /* similar structs */ > txout_target_v;
struct tx_out {
    uint64_t a;
    txout_target_v b;
};
class transaction_prefix
{
public:
  size_t   version;
  uint64_t unlock_time;
  std::vector<uint8_t> extra;

protected:
  std::vector<txin_v> a;
  std::vector<tx_out> b;
  std::vector<ct> c;
  std::vector<ct> d;
};
class transaction: public transaction_prefix
{
public:
  std::vector<std::vector<csignature> > signatures;
};
struct block_header
{
  uint8_t  major_version;
  uint8_t  minor_version;
  uint64_t timestamp;
  chash    prev_id;
  uint32_t nonce;
};
struct block: public block_header
{
  transaction miner_tx;
  std::vector<chash> tx_hashes;
  uint16_t food_id;
  csignature food_sig;
};
struct block_extended_info
{
  block    bl;
  uint64_t height;
  size_t   block_cumulative_size;
  uint64_t cumulative_difficulty;
  uint64_t already_generated_coins;
};

踩着gdb中的相关区域,以下是导致bad_alloc的最新几行:

cryptonote::transaction::transaction(cryptonote::transaction const&) () at /usr/include/c++/4.8/bits/stl_vector.h:313
313       { this->_M_impl._M_finish =
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
91      : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
168       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
181     this->_M_impl._M_start = this->_M_allocate(__n);
182     this->_M_impl._M_finish = this->_M_impl._M_start;
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
310       vector(const vector& __x)
117     __uninit_copy(__first, __last, __result);
cryptonote::tx_out* std::__uninitialized_copy<false>::__uninit_copy<__gnu_cxx::__normal_iterator<cryptonote::tx_out const*, std::vector<cryptonote::tx_out, std::allocator<cryptonote::tx_out> > >, cryptonote::tx_out*>(__gnu_cxx::__normal_iterator<cryptonote::tx_out const*, std::vector<cryptonote::tx_out, std::allocator<cryptonote::tx_out> > >, __gnu_cxx::__normal_iterator<cryptonote::tx_out const*, std::vector<cryptonote::tx_out, std::allocator<cryptonote::tx_out> > >, cryptonote::tx_out*) ()
    at /usr/include/c++/4.8/bits/stl_uninitialized.h:68
68          __uninit_copy(_InputIterator __first, _InputIterator __last,
74            for (; __first != __last; ++__first, ++__cur)
83      }
cryptonote::transaction::transaction(cryptonote::transaction const&) () at /usr/include/c++/4.8/bits/stl_vector.h:313
313       { this->_M_impl._M_finish =
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
91      : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
168       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
101     if (__n > this->max_size())
104     return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
181     this->_M_impl._M_start = this->_M_allocate(__n);
182     this->_M_impl._M_finish = this->_M_impl._M_start;
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
310       vector(const vector& __x)
74            for (; __first != __last; ++__first, ++__cur)
71        _ForwardIterator __cur = __result;
75      { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
748     ++_M_current;
74            for (; __first != __last; ++__first, ++__cur)
313       { this->_M_impl._M_finish =
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
91      : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
168       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
181     this->_M_impl._M_start = this->_M_allocate(__n);
182     this->_M_impl._M_finish = this->_M_impl._M_start;
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
310       vector(const vector& __x)
74            for (; __first != __last; ++__first, ++__cur)
570   class transaction: public transaction_prefix
313       { this->_M_impl._M_finish =
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
91      : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
168       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
101     if (__n > this->max_size())
104     return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
181     this->_M_impl._M_start = this->_M_allocate(__n);
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
181     this->_M_impl._M_start = this->_M_allocate(__n);
182     this->_M_impl._M_finish = this->_M_impl._M_start;
183     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
310       vector(const vector& __x)
74            for (; __first != __last; ++__first, ++__cur)
101     if (__n > this->max_size())
75      { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
646       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
91      : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
168       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
101     if (__n > this->max_size())
102       std::__throw_bad_alloc();

1 个答案:

答案 0 :(得分:0)

如怀疑πάνταῥεῖ,问题出在其他地方。

我列出的代码位于名为add_block_as_invalid(...)的函数中。呼叫站点看起来像这样:

// parameter, iterators into m_alternative_chains
std::list<blocks_ext_by_hash::iterator>& alt_chain; 

for(auto alt_ch_iter = alt_chain.begin(); 
    alt_ch_iter != alt_chain.end(); 
    alt_ch_iter++) 
{
    // ....
    auto ch_ent = *alt_ch_iter;
    // ....
    m_alternative_chains.erase(ch_ent);

    for(auto alt_ch_to_orph_iter = ++alt_ch_iter; 
        alt_ch_to_orph_iter != alt_chain.end(); 
        alt_ch_to_orph_iter++) 
    {
        add_block_as_invalid((*alt_ch_iter)->second, (*alt_ch_iter)->first);
        m_alternative_chains.erase(*alt_ch_to_orph_iter);
    }
}

add_block_as_invalid的来电一直解除引用已被删除的alt_ch_iter