-1

so I get this error when i run my code

Error in module (TraCIDemoRSU11p) RSUExampleScenario.rsu[0].appl (id=8) at event #4000, t=45.40151998544: std::length_error: basic_string::_M_create. TRAPPING on the exception above, due to a debug-on-errors=true configuration option. Is your debugger ready?

it shows that I have this issue at this line after looping multiple times

 VehTD = *iteh2;

please be informed that I'm trying to remove from this vehicle list to another vehicle list and delete the moved items.

 std::list<std::pair<std::string,std::string>>::const_iterator iteh2 = waitingList.begin();
      for (std::list<std::pair<std::string,std::string>>::const_iterator iteh = waitingList.begin(); iteh != waitingList.end() && !waitingList.empty(); ){
          iteh2 = iteh;
          cout<<"Veh ID TF 1:";
          cout<<VehTD.first<<endl;
          cout<<VehTD.second<<endl;
          VehTD = *iteh2;
          cout<<"Veh ID TF 2:";
          cout<<VehTD.first<<endl;
          cout<<VehTD.second<<endl;
          CompareResult3 = Locks(Locked,VehTD.second);
          if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
           //remove from waiting and add to crossing
              std::string ehk = "";
              simtime_t VehicleT = simTime();
              std::pair<std::pair<std::string, std::string>, simtime_t> VehicleWithTime;
              crossingList.push_back(VehTD);


              iteh2 = waitingList.erase(iteh2);
              }
              else{
                  ++iteh;
              }
       }
   }

I'm using Oment++ 5.0 and veins 4.4

I put a try and catch at this function, and it seems that the exception happens towards the end of the list.

Update: I have this output:

WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
WCounter 3
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception

The WCounter is a variable to show how many times it looped before having the exception at the point explained earlier.

based on request I added the locks function:

  std::pair<std::list<std::string>,int> TraCIDemoRSU11p::Locks(std::list<std::string> alreadyLocked, std::string laneNo){
    bool debugL;
    //create variables for the lanes for easier use
try{
      debugL = false;
      zero = "171270266#0_0";
      one = "171270266#0_1";
      two = "-171270025#1_0";
      three = "-171270025#1_1";
      four = "-171270266#1_0";
      five = "-171270266#1_1";
      six = "171270025#0_0";
      seven = "171270025#0_1";



      //create lists of locks

      Locks0 = Locks1 = Locks2 = Locks3 = Locks4 = Locks5 = Locks6 = Locks7 = RequestToLock = {};
      CounterOfSimilarLanes = 0;

      //set of 0 ,Locks0;
      Locks0.push_back(zero);
      Locks0.push_back(five);
      Locks0.push_back(seven);
      //set of 1 ,Locks1;
      Locks1.push_back(one);
      Locks1.push_back(three);
      Locks1.push_back(six);
      //set of 2 ,Locks2;
      Locks2.push_back(one);
      Locks2.push_back(two);
      Locks2.push_back(seven);
      //set of 3 ,Locks3;
      Locks3.push_back(zero);
      Locks3.push_back(three);
      Locks3.push_back(five);
      //set of 4 ,Locks4;
      Locks4.push_back(one);
      Locks4.push_back(three);
      Locks4.push_back(four);
      //set of 5 ,Locks5;
      Locks5.push_back(two);
      Locks5.push_back(five);
      Locks5.push_back(seven);
      //set of 6 ,Locks6;
      Locks6.push_back(three);
      Locks6.push_back(five);
      Locks6.push_back(six);
      //set of 7 ,Locks7;
      Locks7.push_back(one);
      Locks7.push_back(four);
      Locks7.push_back(seven);
      //This is the request to lock from the vehicle using its lane number
      if (laneNo == zero ){
          RequestToLock.insert(RequestToLock.end(),Locks0.begin(),Locks0.end());
      }else if (laneNo == one){
          RequestToLock.insert(RequestToLock.end(),Locks1.begin(),Locks1.end()) ;
      }else if (laneNo == two){
          RequestToLock.insert(RequestToLock.end(),Locks2.begin(),Locks2.end()) ;
      }else if (laneNo == three){
          RequestToLock.insert(RequestToLock.end(),Locks3.begin(),Locks3.end()) ;
      }else if (laneNo == four){
          RequestToLock.insert(RequestToLock.end(),Locks4.begin(),Locks4.end()) ;
      }else if (laneNo == five){
          RequestToLock.insert(RequestToLock.end(),Locks5.begin(),Locks5.end()) ;
      }else if (laneNo == six){
          RequestToLock.insert(RequestToLock.end(),Locks6.begin(),Locks6.end()) ;
      }else if (laneNo == seven){
          RequestToLock.insert(RequestToLock.end(),Locks7.begin(),Locks7.end()) ;
      }

}   //if the already locked from the controller is empty so we take the requested lock
catch(const std::exception &e){
    cout<<"Preparing Locks Algorithm Exception"<<endl;
}
try{
    if (alreadyLocked.empty()){

          markOfLocks = 0;
          alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
          Locked.insert(Locked.end(),RequestToLock.begin(),RequestToLock.end()) ;
          if (debugL == true){
              cout<<"First Lock in the locking algorithm"<<endl;
            //  dumplistLock(Locked);
          }
          ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);

      }else{ // if the already locked by the controller isnt empty, so we need to compare the request with it
          //Search for similar items in the 2 lists
          for(std::list<std::string>::/*const_*/iterator it_1 = alreadyLocked.begin();it_1 != alreadyLocked.end();)
          {
              for(std::list<std::string>::/*const_*/iterator it_2 = RequestToLock.begin(); it_2 != RequestToLock.end();)
              {
                  if(*it_1 == *it_2)
                  {
                      ++CounterOfSimilarLanes;
                  }

                  ++it_2;

              }
                  ++it_1;
          }


          if (RequestToLock == alreadyLocked){
              markOfLocks = 1;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"Same Lane"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);

          }else if(CounterOfSimilarLanes == 0){ //if the request is concurrent meaning totally different locks
              markOfLocks = 2;
              //alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"Concurrent Lane"<<endl;
                  dumplistLock(Locked);
              }

          }else if(CounterOfSimilarLanes == 1  ||  CounterOfSimilarLanes == 2){ // if there's a similarity is 1 lock at least so it leads to conflict
              markOfLocks = 3;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<< "opposite Lane"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
          }else{
              markOfLocks = 1;
              Locked.clear();
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"default case"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
         }

      }
}catch(const std::exception &e){
    cout<<"Lock Cases Exceptions"<<endl;
}

try{
      return ReturnOfLocks;
}catch(const std::exception &e){
    cout<<"No Return of Locks in Locks Alg Exception"<<endl;
}

}
Hadeel A.
  • 19
  • 8
  • What is `VehTD` declaration? – Jerzy D. Sep 20 '18 at 10:10
  • it's std::pair VehTD ; – Hadeel A. Sep 20 '18 at 10:17
  • That's the MCVE. I have the issue with the loop itself, and nothing more. Compare result and Locks function is what makes me decide if i'm going to remove the item or not, and Locks function works perfectly fine. – Hadeel A. Sep 22 '18 at 16:45
  • This is not an MCVE at all, because you have entire functions, types, and classes that you use in the code but never show the declaration of. Particularly suspicious is `Locks()`; if it "works perfectly fine", then you should have no problem showing the people you want to answer, right? It might only *seem* to work fine but cause some undefined behaviour that only manifests later. Consequences and causes don't always accompany each other in a language like C++ where undefined behaviour is a thing. – underscore_d Sep 22 '18 at 17:19
  • I added the locks function as per to your request, so kindly inform me if there's an issue. I did use that function in other places, but it didn't seem to cause a problem earlier thus i assumed it works fine. – Hadeel A. Sep 22 '18 at 17:34

2 Answers2

0

You should use iterator instead of const_iterator because you modify (i.e. delete) indicated element.
By the way, in the code iteh2 is unnecessary - iteh is sufficient as well as VehicleT and ehk variables are not used and may be removed.

Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
  • i changed, it still showed me the same error. I'll generate another sumo scenario and try it, and comment if the issue is in the same place or generated by another piece of code running at the same time with this one. – Hadeel A. Sep 20 '18 at 12:50
  • after generating a different scenario, it showed me this: No source available for "wcstok_s() at 0x75eba934" Program received signal SIGSEGV, Segmentation fault. 0x75eba934 in wcstok_s () from C:\WINDOWS\System32\msvcrt.dll what is it supposed to mean though? – Hadeel A. Sep 20 '18 at 12:58
  • 2
    You forgot to mention how any of these points relate to the problem in the question... These seem like good points, which would make for a good comment, but I don't see how they constitute an answer. – underscore_d Sep 22 '18 at 17:16
  • @underscore_d: I have rewritten my answer. I suspect that using of `const_iteratror` may be an issue, but I'm not able to reproduce the error from question because the presented code does not contain all variables and methods. – Jerzy D. Sep 23 '18 at 14:10
  • `const_iterator` only prevents dereferencing to a non-`const` reference and modifying the element; it does not (anymore, after a Standard defect got fixed) prevent calling member functions like `.erase()` that don't actually modify or reassign elements (just destroy them). See [this answer/thread](https://stackoverflow.com/a/4888697/2757035). So, I don't see how using a `const_iterator` here can be a problem at all, and in fact, it's better stylistically and for safety if the user indeed does not want to modify elements while looping. – underscore_d Sep 23 '18 at 15:41
0

so I changed the for loop to a while loop and it didn't show an exception, and not sure why.

therefore the loop looks like this now:

     //while loop instead.
   std::list<std::pair<std::string,std::string>>::iterator iterwil = waitingList.begin();
   std::pair<std::string, std::string> Vehwil ;
   while (iterwil != waitingList.end()){
       Vehwil = *iterwil;
       CompareResult3 = Locks(Locked,Vehwil.second);
       if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
           crossingList.push_back(Vehwil);
           waitingList.erase(iterwil++);
       }else{
           ++iterwil;
       }

   }

if someone has a valid reason to enlighten me on why the while loop works with no exception unlike the for loop that would be great. ^^"

Hadeel A.
  • 19
  • 8
  • That is very different code, not just the change in loops. Your original code probably didn't work because for some unknown reason you were working with 2 iterators, which were seemingly meant to be copies of each other - but you were actually comparing `iteh` to `end`, but only incrementing `iteh2`, so that `for` loop would have never terminated, which (just speculating; I'm not going to analyse the flow) might have led to infinite `push_back()`s and an out-of-memory condition. In any case, the original code was never going to work. – underscore_d Sep 23 '18 at 15:46
  • This `waitingList.erase(iterwil++);` is wrong. You want to continue from the returned iterator so use: `iterwil = waitingList.erase(iterwil);` instead. – Alexis Wilke Jul 21 '22 at 04:02