0

I am working on Veins framework, inside OMNET++. I am developing a kind of controlled flooding in a vehicular network. I am trying to make the vehicles send the data only after 6 seconds (this value may be changed), so I created a schedule at the initialization of each vehicle (on TraCIDemo11p.cc), like this:

if(sendData){ scheduleAt(simTime()+par("dataInterval").doubleValue(),sendDataEvt);}

On the TraCIDemo11p.h I created a map to be my buffer:

//Creating a map to represent the vehicle's message buffer
typedef map<string, WaveShortMessage*> MyMapContextMessageBuffer;
typedef pair<string, WaveShortMessage*> MyPairContextMessageBuffer;
MyMapContextMessageBuffer contextLocalMessageBuffer;

So on the TraCIDemo11p.cc onData(WaveShortMessage* wsm) function I make a copy of the received message, update the number of received messages, add the message in the vehicle buffer and print the content of the buffer just to test if I inserted it correctly:

//duplicating the message, so this vehicle has his own copy of the message
WaveShortMessage* wsmdup = wsm->dup();

//number of received messages
receivedMsgs++;

//add the msg in the  vehicle buffer, the GlobalMessageIdentification is the id of the message
contextLocalMessageBuffer.insert(MyPairContextMessageBuffer(wsmdup->getGlobalMessageIdentificaton(),wsmdup));

//accessing something in the ->second on the buffer, it works in this function
cout<<"Sender "<< contextLocalMessageBuffer.begin()->second->getSender()<<endl;

Then at the scheduled time the SEND_DATA_EVT (handleSelfMsg) is called to send the data, but I am getting an error when i try to access the contextLocalMessageBuffer.begin()-second (or it->second).

//handleSelfMsg(cMessage* msg)
case SEND_DATA_EVT 

//if the vehicle has received a msg before
if(receivedMsgs != 0){
  //and the buffer isn't empty, the function is able to verify if the buffer isnt empty
  if(!contextLocalMessageBuffer.empty()){


    //if i try to access the begin->first it works    
    cout<<"GlobalMessageID "<<contextLocalMessageBuffer.begin()->first<<" ";
    //but if I try to access the begin->second->getSource(), it doesn't works in this function (i get an Error 139), but works inside of onData    
    cout<<findHost()->getFullName()<<" messageID "<<contextLocalMessageBuffer.begin()->first<<" "<<contextLocalMessageBuffer.begin()->second->getSource()<<endl;

    //if I remove those couts the iterator and the for works but the send doesn't (Error 139), because it is trying to access the it->second
    map<string, WaveShortMessage*>::iterator it;
    for(it = contextLocalMessageBuffer.begin(); it != contextLocalMessageBuffer.end(); it++){

      send(it->second)
    }    
  }
}
//create a new schedule to send data after a new interval
scheduleAt(simTime() + par("dataInterval").doubleValue(), sendDataEvt);

So, to summarize, my problem is that i am able to work with my map(contextLocalMessagebuffer) inside of the onData, but can't access it properly on HandleSelfMsg, both in the same code (TraCIDemo11p.cc). Obs: I tried to create a function that returns the value of the buffer, but the function wasn't able to access the buffer too.

Thanks!

Sharath
  • 691
  • 8
  • 23
W.Junior
  • 13
  • 5
  • One thing that's odd is that you define an object in a header file, which should usually only contain declarations. This is going to give you issues if you include this header file more than once. Further than that, can you extract a minimal example? – Ulrich Eckhardt Dec 11 '15 at 19:03
  • @UlrichEckhardt Do you mean a minimal example of the whole application or only of what i described above? – W.Junior Dec 11 '15 at 20:14
  • If you can run your simulation on Linux or MacOS, give valgrind (memcheck) a try. It is the perfect tool for finding such problems. – Christoph Sommer Dec 12 '15 at 09:02
  • @ChristophSommer i'm gonna try that, thanks for the answer, i will post here what i find. – W.Junior Dec 13 '15 at 15:41
  • I changed the content of my map and instead of adding the wsm to the buffer i made a test adding only a int and the index, like this: map msgBuffer; So i am able to work with the map in all functions, but i need the information from the wsm to send it. I'm gonna try to run Valgrind like you @ChristophSommer described in this post: http://stackoverflow.com/questions/29894898/finding-a-memory-allocation-error-with-omnet – W.Junior Dec 14 '15 at 16:51
  • [Problem solved] I was "debugging" with my friend yesterday and he realized that some pointers (*) were being used improperly. I changed them and some '->' became '.' so now the code is working. UlrichEckhardt and Christoph Sommer thanks for the help. – W.Junior Dec 15 '15 at 12:40
  • Maybe, you want to post your solution to the question as a dedicated answer here, such that the question will be marked answered and hence be more effective for helping people with a similar problem. – Julian Heinovski Oct 10 '17 at 14:12

0 Answers0