我在使用地图做任何事情时都会出现分段错误。它的声明如下:
typedef std::map<uint16_t, std::vector<std::pair<std::string, uint16_t> > > bitMap_t;
然后我有一个使用此地图的私人班级成员
private:
bitMap_t m_bitMap;
使用此地图的代码如下所示
uint16_t i = 1;
uint16_t j = 0;
uint16_t reg = 0;
uint16_t regIndex = 0;
uint8_t regSizeWords = 0;
for (regIndex = 0; regIndex < NUMBER_OF_BIT_REGS; regIndex++) {
for (i = 1, reg = m_regList[regIndex].address; reg < (m_regList[regIndex].address + REGS_SIZE); reg++, i++) {
for (j = 0; j < m_regList[regIndex].signalsAndBitMasks.size(); j++) {
ostringstream signalName;
signalName << m_regList[regIndex].signalsAndBitMasks[j].first << "." << i;
m_bitMap[reg].push_back(make_pair(signalName.str(), m_regList[regIndex].signalsAndBitMasks[j].second));
}
}
}
我还测试了在所有这些代码之前执行m_bitMap.clear()并且也没有用。所以我猜它与嵌套地图,矢量和配对有关? segfault错误如下所示:
std::less<unsigned short>::operator() at stl_function.h:230 0x805e8da
std::_Rb_tree<unsigned short, std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > >, std::_Select1st<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > >, std::less<unsigned short>, std::allocator<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > > >::_M_lower_bound() at stl_tree.h:986 0x8063a0f
std::_Rb_tree<unsigned short, std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > >, std::_Select1st<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > >, std::less<unsigned short>, std::allocator<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > > >::lower_bound() at stl_tree.h:745 0x8063531
std::map<unsigned short, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > >, std::less<unsigned short>, std::allocator<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > > >::lower_bound() at stl_map.h:701 0x806311a
std::map<unsigned short, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > >, std::less<unsigned short>, std::allocator<std::pair<unsigned short const, std::vector<std::pair<std::string, unsigned short>, std::allocator<std::pair<std::string, unsigned short> > > > > >::operator[]() at stl_map.h:447 0x8062bc1
srvmgr::MbBitMap::InitBitMap() at mbbitmap.cpp:175 0x80620cc
答案 0 :(得分:1)
到目前为止,最常见的原因是实际上并没有真正拥有地图。
您写的是地图是private: bitMap_t m_bitMap;
,即对象的成员。我敢打赌你有一个指向这个对象的指针(MbBitMap
?),但由于某些原因与std::map
无关,这个指针无效。