error reading variable: cannot access memory at address

2.5k views Asked by At

I created a vector of lists of some class.

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

Then I filling this vector for example like this:

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

There is some situation when I erasing some lists in 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";
}

After this when running, I receive an error warning from the debugger: error reading variable: cannot access memory at address ...

Debugger points at that code at: 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);
                            }
                        }
                    }

So when I'm accessing: *iter, there comes some error of memory access. I think, that there is something wrong with erasing list in vector. Any ideas?

0

There are 0 answers