错误读取变量:无法访问地址处的内存

时间:2015-06-08 12:20:53

标签: c++ list memory vector

我创建了一个类的列表向量。

std::vector< std::list<LAddress::L2Type> > RouteCache;

然后我填写这个矢量,例如:

std::list<LAddress::L2Type> RecvdRoute = NetwPkt->getRoute();
RecvdRoute.push_front(NetwPkt->getSrc());
RecvdRoute.reverse();
RouteCache.push_back(RecvdRoute);

当我在vector中删除一些列表时会出现一些情况:

std::vector<std::list<LAddress::L2Type> >::iterator SearchIt;
std::vector<std::list<LAddress::L2Type> >::iterator it;
bool founded = false;
for(it = RouteCache.begin(); it != RouteCache.end(); it++)
{
    for(std::list<LAddress::L2Type>::iterator iter = it->begin();
        iter != it->end(); 
        iter++)
    {
        if((*iter == NetwPkt->getSrc()) && (*iter != it->back()))
        {
            if (debug) EV << "Founded an entry: " << NetwPkt->getSrc() << "\n";
            std::list<LAddress::L2Type>::iterator CIT = iter;
            CIT++;
            if (debug) EV << "Next point: " << *CIT << " \n";
            if(*CIT == NetwPkt->getReplRoute().front())
            {
                if (debug) EV << "Broken point is " << *CIT << " \n";
                SearchIt = it;
                founded = true;
            }
        }
    }
}

if(founded)
{
    EV << "Erasing a wrong route!\n";
    SearchIt->clear();
    RouteCache.erase(SearchIt);
    EV << "Now route cache size: " << RouteCache.size() << " \n";
}

运行后,我收到调试器的错误警告:error reading variable: cannot access memory at address ...

调试器指向该代码:if(*iter == SrvrAddr)

bool HaveARoute = false;
if(SrvrAddr != LAddress::L2NULL)
{
    std::vector< std::list<LAddress::L2Type> >::iterator it;
    for(it = RouteCache.begin(); (it != RouteCache.end()) && (!HaveARoute); it++);
    {
        //std::list<LAddress::L2Type>::iterator iter;
        for(std::list<LAddress::L2Type>::iterator iter = it->begin(); (iter != it->end()) && (!HaveARoute); iter++)
        {
            if(*iter == SrvrAddr)
            {
                if (debug) EV << "I have a route to a server!\n";
                NetwPkt->getRoute().push_back(MyAddr);
                NetwPkt->setReplRoute(NetwPkt->getRoute()); //placing to reply route already accumulated route
                std::list<LAddress::L2Type>::iterator CIT = iter;
                CIT++;
                for(std::list<LAddress::L2Type>::iterator iterat = it->begin(); iterat != CIT; iterat++)
                {
                    NetwPkt->getReplRoute().push_back(*iterat); //pushing to reply route a part of route which is knowing by us
                }
                std::vector< std::list<LAddress::L2Type> >::iterator i; //now we must to check if we have a route to the source of received packet
                bool founded = false;
                for(i = RouteCache.begin(); (i !=RouteCache.end()) && !founded; i++)
                {
                    for(std::list<LAddress::L2Type>::iterator ite = i->begin(); (ite != i->end()) && !founded; ite++)
                    {
                        std::list<LAddress::L2Type>::iterator CITER = ite;
                        CITER++;
                        if(*ite == NetwPkt->getSrc())
                        {
                            EV << "I have already known route to this node or know a route which contains that node!\n";
                            founded = true;
                            NetwPkt->getRoute().clear();
                            for(std::list<LAddress::L2Type>::iterator IT = i->begin(); IT != CITER; IT++)
                            {
                                NetwPkt->getRoute().push_back(*IT);
                            }
                        }
                    }

因此,当我访问:*iter时,会出现内存访问错误。 我认为,在向量中擦除列表有问题。 有什么想法吗?

0 个答案:

没有答案