0

I wrote an application for a vehicle in Veins as follows:

The header file:

class CarApp : public BaseWaveApplLayer
{
public:
  CarApp();
  ~ CarApp();
  void initialize(int stage);
  void finish();

protected:
  void handleSelfMsg(cMessage* msg);
  void onBSM(BasicSafetyMessage* bsm);

private:
  std::vector<Sender> senderVector_;
};

The implementation:

CarApp::CarApp() {}
CarApp::~CarApp() {}

void CarApp::initialize(int stage) {
  BaseWaveApplLayer::initialize(stage);
}

void CarApp::finish() {
  BaseWaveApplLayer::finish();
}

void handleSelfMsg(cMessage* msg) {
  BaseWaveApplLayer::handleSelfMsg(msg);
}

inline Sender* findSender(int senderId, std::vector<Sender>& senderVector) {
  for (auto sender : senderVector) {
    if (sender.getId() == senderId)
      return &sender;
  }
  return nullptr;
}

void onBSM(BasicSafetyMessage* bsm) {
  if (condition to check if this BSM is from a new sender) {
    auto sender = Sender(bsm->getSenderAddress(), other variable initializations);
    senderVector_.push_back(sender); // <- this is where I face the problem
  }
  else {
    // update other values in Sender object
  }
  // This part wasn't in the original MWE
  auto sender = findSender(id, senderVector_);
  // ... process members of "sender"
  delete(sender); // <-- this was the real culprit!
}

Sender class header:

class Sender
{
  Sender();
  explicit Sender(int id, Coord pos, Coord accel);

  private:
    int id_;
    Coord pos_;
    Coord accel_;
}

Sender class implementation:

Sender::Sender() {}
Sender::Sender(int id, Coord pos, Coord accel)
  : id_(id), pos_(pos), accel_(accel) {}

Whenever CarApp receives a BSM, the onBSM() function is run.

When I receive the first BSM, a new Sender object is created, initialized and pushed into senderVector_. However, when I receive the next BSM from either the same sender or any other, the previously stored Sender object gets corrupted with garbage values.

Also, CarApp crashes when a new Sender object is being pushed into senderVector_.

I am out of reasons for this to fail since it seems pretty simple to work as expected. Does anyone have any ideas why it is not?

Edit 1: removed references to Coord objects as suggested by @UnholySheep and @user6386155

Edit 2: I wrote a simple MWE that would work without the simulator, just to check the logic. It works flawlessly. This is definitely not a C++ issue but a Veins or OMNET++ issue.

Edit 3: Updated the MWE with the real issue. I found this out later and hence wasn't able to replicate the logic in this MWE. SORRY!

Raashid
  • 149
  • 9
  • 1
    Where are the `Coord` objects created that `pos_` and `accel_` refer to? Are you sure they outlive the lifetime of the corresponding `Sender`? – UnholySheep Jul 09 '18 at 16:54
  • Why do you store references? May be consider shared pointer instead – user6386155 Jul 09 '18 at 17:03
  • @UnholySheep You're right that `Coord` will not outlive `Sender`. I removed the reference to copy those objects into Sender. Even after this `CarApp` crashes. The issue is that even the `id_` variable is getting modified, which is just an `int`. – Raashid Jul 09 '18 at 17:30
  • 1
    This isn't the bug, but: Always follow either the rule of 3 or the rule of 0. – o11c Jul 09 '18 at 19:01
  • you could define the members of Sender as `const`. Might show you what is changing them – user6386155 Jul 09 '18 at 19:03
  • It might also be insightful to run the simulation in valgrind using its memcheck tool. This might show you if some unrelated code is overwriting parts of your memory. – Christoph Sommer Jul 10 '18 at 06:20
  • Did you try storing pointers (`new Sender(bsm->getSenderAddress(), other variable initializations);`) in your data structure? – Julian Heinovski Jul 10 '18 at 08:26
  • I found what the issue was with my code. I did not know where the issue is and hence this MWE did not show what I really have. I have updated it to show what my problem was. Thanks all for your help! – Raashid Jul 11 '18 at 16:31
  • @ChristophSommer you are right. That's what was happening. If anyone could close this, that would be great. – Raashid Jul 11 '18 at 16:56
  • Thanks for contributing both the real problem and the real solution! This might be very helpful for others facing similar problems. Might I suggest writing an answer (and then marking it as the best one)? This way other people facing similar trouble are sure to find it quickly. – Christoph Sommer Jul 11 '18 at 17:18

1 Answers1

0

Posting this answer on @ChristophSommer 's suggestion.

When I posted this question, I did not know the real issue and hence I did not create the MWE that represented the actual code.

I later found out that I was deleting a pointer to an object in the senderVector_. This pointer did not allocate any memory but was merely pointing to the correct Sender object.

The solution was just to remove the delete(sender); line from the code. Since this pointer is created on the stack, it is discarded automatically once onBSM() finishes execution.

Raashid
  • 149
  • 9