Question

I am debugging a code dealing with vectors and iterators. I get an assertion error when I am clicking the "New" button on my GUI. The assertion error is that in the title, with the addition of /vector Line 251.

I have traced the problem to a part of the code attempting to remove an element from a vector. I will post the entire function and then the line that bugs:

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;
}

Now the line that bugs is:

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

The definition of the removeElem function is:

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

    return removeElem1(pMyObj);
    }

The definition of removeElem1 is:

  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;
}

My guess is that the removeElem function is attempting to remove a vector element that isn't there or that is out of the index range, but I am unable to figure out where exactly I can fix this. Any help is appreciated.

Thanks in advance

Was it helpful?

Solution

It's not obvious which particular framework you're using here (Rogue Wave?), but I think it may be possible to deduce the problem.

The key to decoding this assertion is understanding what incompatible iterators means. In general it means that you're trying to do an operation on a pair of items that don't refer to the same thing. For instance: (with standard library containers)

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

If you dig into the definition of the iterator types you have, then you should find that when the iterator is created it stores a reference back to the container it was created from. If you then perform an operation on two iterators (as in the first case above) then it can detect that they refer to different containers and hence aren't compatible. In the second case you have an operation on a container and an iterator, and so again it will assert that the iterator refers to that container.

In your case it appears that you're trying to remove an element from a container. The framework is asserting that the item isn't in the container (and in fact is probably in another). I suspect that you're deleting an item from the wrong container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top