调试断言失败:迭代器无效

时间:2012-03-22 17:26:29

标签: c++ debugging vector assertion

我目前正在使用模拟器并在调试运行时遇到以下错误:表达式:向量不兼容的迭代器

代码如下:

    class Network {
    private:
             vector<Node*> nodes;
             ....
             void parse_config(void);
             ....
    };

在parse_config方法中,我有一个生成错误的序列。就是这样:

    if(nodes.empty()) // add the first node to the network
        {
            Node current(regex_d[1]); // create current(first src) node
            Node *acurrent = &current;  

            Node next_hop(regex_d[2]); // create the node we immediately send to
            Node *anext_hop = &next_hop;

            acurrent->add_next_hop(anext_hop); 

            acurrent->add_n_vchannels(regex_d[5]);

            nodes.push_back(acurrent); // <== error
            nodes.push_back(anext_hop); // <== here as well
        }

这有解决方法吗? 任何帮助/建议/参考将非常感谢。

塞比

1 个答案:

答案 0 :(得分:1)

您的指针指向堆栈对象。虽然这在您的代码中并不明显,但您很可能在节点向量中有一些已经回收的指针。在上面:

Node * acurrent = new Node(regex_d [1]);

至少会使记忆问题更准确。

至于您遇到的问题,可能是内存位置被用于其他东西,导致您的指针指向与节点完全不同的对象。

相关问题