矢量迭代器不兼容矢量线251

时间:2013-11-27 14:17:49

标签: c++ vector iterator assertions

我正在调试处理向量和迭代器的代码。当我点击GUI上的“新建”按钮时,我收到断言错误。断言错误是在标题中,添加/ vector第251行。

我已经将问题追溯到试图从向量中移除元素的部分代码。我将发布整个函数,然后发布错误的行:

int VsuCNTreeNodeManager::deleteTreeNode(RWCString & CNNameToDelete, RWTValSlist<VsuDeletedCN> & deletedCNList)
{
    RWCString childName,  parentName;
    VsuCNTreeNode *pNode;
    int i;
    int size;

    if (!nodeList.contains(CNNameToDelete))
        return 1; // Means that CNNameToDelete doest not exist.

    pNode = ordCNList[nodeList[CNNameToDelete]];

    travForName.reset();
    travForName.processElement(pNode);

    const RWTValSlist<RWCString> & childNameList = travForName.getNameList();

    size = childNameList.entries();

    // If it is the Top node that is deleted then
    // the VsuCNTreeNodeManager's top node pointer is reset.

    if ( pNode == pTopCNTreeNode )
    {
        pTopCNTreeNode = NULL;
    }

    for ( i = 0; i < size; i++)
    {
        //******* How would it possible to have a name not contained in the nodeList
        //******* since it has been extracted from the nodeList ?????????????

        childName = childNameList.at(i);

        if (nodeList.contains(childName))
        {
            //******* Process that get the Parent List of each deleted Tree Node
            //******* The following code unref all the Tree Nodes that was referencing any deleted Tree Node

            pNode = ordCNList[nodeList[childName]]; // Get the Tree Node to be deleted

            // Fill the deletedCNList
            deletedCNList.insert( VsuDeletedCN(childName, pNode->getCN()->hasType()) );

            VsuDependencyRemoverVisitor visitor( *pNode );

            for (unsigned int k = 0; k < pNode->getParentList().entries(); k++)
            {
                parentName = pNode->getParentList().at(k)->getCN()->getName();

                if ( nodeList.contains(parentName) ) // Check if the parent is not deleted
                {
                    //*** Remove the reference of the deleted tree node from that parent
                    RWBoolean status;
                                        status = ordCNList[nodeList[parentName]]->removeElem(childName); //                           Removing the reference that pNode(parent) had on key(Child)

                }
            }

            //******* Remove references on this object from observers.

            pNode->resetObserverFlags();
            pNode->updateAllObservers(&visitor);

            //******* Process that delete all the Tree Nodes in the parentList

            nodeList.remove(childName);
        }
    }

    //*****************update Lists********************

    size = ordCNList.entries();
    int index = 0;

    RWTValHashDictionary<RWCString, int> tmpNodeList(rwhash);
    //nodeList.clear();

    RWTPtrOrderedVector<VsuCNTreeNode> nodeToDelete(childNameList.entries());

    for(i = 0; i < size; i++)
    {
        pNode = ordCNList[index];
        childName = pNode->getCN()->getName();

        if (!childNameList.contains(childName))
        {
            tmpNodeList.insertKeyAndValue(childName, index);
            index++;
        }
        else
        {
            ordCNList.remove(pNode);
            typeList[pNode->getCN()->hasType()].treeNodeList.remove(pNode);

            // Decrement type counter and if it reach 0 then
            // the entry is removed.
            if( !typeList[pNode->getCN()->hasType()].treeNodeList.entries() )
                typeList.remove(pNode->getCN()->hasType());

            nodeToDelete.insert(pNode);

        }
    }
    nodeList.clear();
    nodeList = tmpNodeList;


    ordCNList.resize(index);

    if (!index)
        pTopCNTreeNode = NULL;

    for( unsigned int j=0; j < nodeToDelete.entries(); j++)
    {
        delete nodeToDelete[j];
    }

    return 0;
}

现在错误的一行是:

      RWBoolean status;
      status = ordCNList[nodeList[parentName]]->removeElem(childName);

removeElem函数的定义是:

 RWBoolean VsuVE_Collection::removeElem(const RWCString & data)
    {
    VsuVE_Moveable *pMyObj = elementList.at(nameList[data]);

    return removeElem1(pMyObj);
    }

removeElem1的定义是:

  RWBoolean  VsuVE_Collection::removeElem1(VsuVE_Moveable *elem)
{
    if (elementList.remove(elem) == FALSE) // THE ASSERTION ERROR HAPPENS RIGHT HERE
        return FALSE;

    //**** Reordering the nameList
    nameList.clear();
    int size = elementList.entries();
    for (int i = 0; i < size; i++)
    {
        nameList.insertKeyAndValue(elementList.at(i)->name, i);
    }

    return TRUE;
}

我的猜测是removeElem函数正在尝试删除不存在或不在索引范围内的向量元素,但我无法弄清楚我能在哪里解决这个问题。任何帮助表示赞赏。

提前致谢

1 个答案:

答案 0 :(得分:0)

你在这里使用哪个特定框架(Rogue Wave?)并不明显,但我认为可以推断出这个问题。

解码此断言的关键是理解不兼容的迭代器的含义。一般来说,这意味着你试图对一对不引用相同内容的项目进行操作。例如:(使用标准库容器)

std::vector<int> v1, v2;
for (auto it=v1.begin(); it!=v2.end(); it++) {  // <=== iterator incompatible
}

std::vector<int>::iterator it1=v1.begin();
v2.erase(v1); // <==== iterator incompatible

如果深入研究你所拥有的迭代器类型的定义,那么你会发现在创建迭代器时它会将引用存储回创建它的容器。如果然后对两个迭代器执行操作(如上面的第一种情况),那么它可以检测到它们引用不同的容器,因此不兼容。在第二种情况下,您对容器和迭代器进行了操作,因此它将再次声明迭代器引用该容器。

在您的情况下,您似乎正在尝试从容器中删除元素。该框架断言该项目不在容器中(实际上可能在另一个容器中)。我怀疑你是从错误的容器中删除了一个项目。