简体   繁体   English

矢量迭代器不兼容:运行时错误

[英]Vector iterators incompatible: runtime error

I'm currently having trouble with a destructor of a class which contains a vector of objects. 我目前在使用包含对象向量的类的析构函数时遇到问题。 The application runs fine, however upon freeing the heap it throws an error. 应用程序运行正常,但是在释放堆时它会抛出错误。

Here is the code of my destructor: 这是我的析构函数的代码:

    ~StaticNetwork(void) { // clear memory
    for(vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); )
        nodes.erase(iter++);
}

And nodes are being added to the network as follows: 节点正在添加到网络中,如下所示:

    if((temp = is_already_added(regex_d[1])) >= 0) // check if the src node has already been added
            {
                if((temp1 = is_already_added(regex_d[2])) >= 0) // check if the next_hop has already been added
                {
                    nodes[temp]->add_n_vchannels(regex_d[5]);
                    nodes[temp]->add_next_hop(nodes[temp1]);
                }
                else // the next_hop has not been added 
                {
                    Node *anext_hop = new Node(regex_d[2]);

                    nodes[temp]->add_next_hop(anext_hop);
                    nodes[temp]->add_n_vchannels(regex_d[5]);

                    nodes.push_back(anext_hop); //  add next hop        
                    param.n_of_nodes++;
                }       
            }

The network is comprised of pointers to the actual nodes. 网络由指向实际节点的指针组成。

Any help/suggestion/reference/(constructive)criticism will be greatly appreciated. 任何帮助/建议/参考/(建设性)批评将不胜感激。

Your iteration over the container is wrong. 您对容器的迭代是错误的。 If the node is a member of the class, then ignore it as the destructor of the vector will take care of it. 如果node是类的成员,则忽略它,因为向量的析构函数将处理它。 If it is not a member and you really want to remove all elements, the simplest thing is calling node.clear() (Note both are equivalent to your code, but they will leak the pointed memory if it should be managed by your class) 如果它不是成员并且你真的想要删除所有元素,最简单的事情就是调用node.clear() (注意两者都等同于你的代码,但是如果应该由你的类管理它们会泄漏指向的内存)

If the pointers are managed by your class, consider using smart pointers or specific pointer containers. 如果指针由您的类管理,请考虑使用智能指针或特定指针容器。 Else the simplest loop to free all memory would be: 另外,释放所有内存的最简单循环是:

for ( std::vector<Node*>::iterator it = nodes.begin(); it != nodes.end(); ++it )
   delete *it;

Note that I did not modify the container itself, just the contained elements. 请注意,我没有修改容器本身,只修改了包含的元素。

You do not need to delete elements of vector manually, it will be done by vector itself. 您不需要手动删除矢量元素,它将由矢量本身完成。 This is how destructors work: they call destructors of member objects of deleted object, so you don't have to worry about it. 这就是析构函数的工作方式:它们调用已删除对象的成员对象的析构函数,因此您不必担心它。

erase doesn't work as you expect: it removes the elements from the container, ie the pointers and not the pointed object. 擦除不能按预期工作:它从容器中删除元素 ,即指针而不是指向的对象。 So you are leaking memory here. 所以你在这里泄漏记忆。

Moreover, erase invalidates the iterators following the erased element(s), thus the test iter != nodes.end(); 此外,擦除使擦除元素之后的迭代器无效,因此测试iter != nodes.end(); causes the error, as you increment the pointer past it. 当您将指针递增过来时会导致错误。

Anyway, you can write the code as shown by David Rodríguez - dribeas. 无论如何,您可以编写DavidRodríguez所示的代码 - dribeas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM