清除已经空的向量会导致未定义的行为吗?

时间:2011-09-28 15:34:11

标签: c++ vector undefined-behavior std

如果clear之前vector *** glibc detected *** process.out: double free or corruption (out): 0x01a0b588 *** 已被清除,会怎样? 我在Visual Studio中尝试过,它没有导致任何运行时错误。但是我正在寻找可能导致异常的原因(下图)并且想知道这是否是原因?

bool myclass::Sort(SortType enSortOption)
{

  bool bRet = TRUE;


  m_oVecOfIntsOrig.clear();
  m_oVecOfIntsSorted.clear();

  for(int i = 0;i<m_oList.listsize();i++)
  {
    m_oVecOfIntsOrig.push_back(i);
  }
  m_oVecOfIntsSorted = m_oVecOfIntsOrig; 
  //Just sort the indices     
  switch(enSortOption)
  {
  case Alpha:
    { 
      t_SortStructAlpha sSortAlpha(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortAlpha );        
    }
    break;
  case Dist:
    {        
      t_SortStructDist sSortDist(this);
      sort( m_oVecOfIntsSorted.begin(), m_oVecOfIntsSorted.end(), sSortDist );
    }
    break;
  case none:  
    {

      //Nothing to do
      return TRUE;
    }
    break;  
  default:
    {
      bRet = FALSE;

    }
    break;
  }

  //Update the list based on sorted index vector
  ListElem oSortedListElements;
  //this clear function simply clears all the 7 constituent vectors. 
  oSortedListElements.vClear();
  if(TRUE == bGetSortedList(oSortedListElements))
  {
    //If successfully copied to temp object, then update member variable
    //m_oList is what I'm intending to display
    m_oList.vClear();
    m_oList = oSortedListElements;
  }
  else
  {
    //copy failed
    bRet = FALSE;

  }
  return bRet;
}


bool myclass::bGetSortedList(ListElem& oSortedListElements)
{

  bool bRet = TRUE;

  //Creating object from beginning, so clear old contents if any
  oSortedListElements.vClear();
  /* Sort done. Now update the list based on new indices*/
  for(int u16Index = 0; u16Index<m_oVecOfIntsSorted.size(); u16Index++)
  {
    if(
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec1.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec2.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec3.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec4.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec5.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec6.size())
      ||
      (m_oVecOfIntsSorted.at(u16Index) >= m_oListConstituents.oVec7.size())
      )
    {
      //out of bounds check
      return FALSE;
    }
    //m_oListConstituents contains overall list
    oSortedListElements.oVec1.push_back(m_oListConstituents.oVec1.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec2.push_back(m_oListConstituents.oVec2.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec3.push_back(m_oListConstituents.oVec3.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec4.push_back(m_oListConstituents.oVec4.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec5.push_back(m_oListConstituents.oVec5.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec6.push_back(m_oListConstituents.oVec6.at(m_oVecOfIntsSorted.at(u16Index)));
    oSortedListElements.oVec7.push_back(m_oListConstituents.oVec7.at(m_oVecOfIntsSorted.at(u16Index)));     

  }

  return bRet;

}

更新代码:

{{1}}

2 个答案:

答案 0 :(得分:6)

如果您所做的事情等于

v.clear();
v.clear();

然后不,没有UB:v.clear()的后置条件之一是v.size() == 0,所以对clear的第二次调用应该是无操作。

答案 1 :(得分:1)

连续两次调用clear方法应该没问题。您的错误看起来像是在释放已经释放的内容。这可能是因为您在同一个对象上调用了free两次,或者可能会在您的向量中放置一个对象,然后将其删除。在向量上调用clear会导致该错误(这是一个问题,因为clear会调用向量中包含的任何对象上的析构函数。)

修改

at会返回一个引用,因此删除设置为at返回的值的内容可能会导致问题。