Search code examples
c++omnet++veins

Previously stored values, in a member vector, change after receiving new BSM


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!


Solution

  • 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.