0

I have been going through the forms to see why this exception is thrown and one of the reasons I see is that pointer is not initialized. I have a callback class which has a pure virtual function for inserting Status into a Map.

 class Motor_callbacks
     {
     public:

        Motor_callbacks() {};
        virtual ~Motor_callbacks() {};

        virtual void insert_status(struct Motor_status) = 0; 
     };

I am using to this callback class in my another class called Station. Which has a child class called Assembly. This is the Header File for Station.

class Station: public Motor_callbacks
     {
     public:

        Station() {};
        virtual ~Station() {};

        std::vector<Assembly*> assembly
        
        int prepare(); 
        int assemble(); 
        void insert_status(struct Motor_status); 
     };

This is the CPP file for Station class

int Station::prepare()
{
  std::vector<Element> children = element.get_children("assembly");
  const uint32_t num_children = children.size();
  if (children.size() != 0)
  {
     for (unsigned int idx = 0; idx < num_children; ++idx)
     {
        Assembly *tmp = new Assembly;
        if (tmp->prepare(children.at(idx)) != 0)
        {
           delete tmp;
           return 1;
        }
        else
        {
           assembly.push_back(tmp);
        }
     }
  }
 }

int Station::assemble()
{assembly.at(i)->set_callback(this);}

 void Station::insert_status(Motor_status _insert_status)
{
// Map Insert
ccsid_Mtrstatus_map.insert(std::pair<uint32_t, Motor_status>(_insert_status.id,_insert_status));}

In the Assembly class I am calling a callback function that points to the Motor_callback class, This is the Header file for Assembly

class Assembly: public Motor_callbacks
     {
     public:

        Assembly() {};
        virtual ~Assembly() {};
        Stator *stator; 
        void set_callback(Motor_callbacks *_mtr_callback);
     };

This is the CPP file for assembly.

int Assembly::prepare()
 {
   stator = new Stator;
     if (stator == nullptr || stator->prepare() != 0)
     {
        error = true;
        delete stator;
        stator = nullptr;
        return 1;
     }
 }

int Assembly::update()
{
  bool error = false;
  if (stator== nullptr || stator->update() != 0)
  {
  error = true;
  return 1;
  }
  else
  {
  Motor_status stator_status_tmp;
  stator_status_tmp.availability_status = stator->stator_avail();
  stator_status_tmp.fault_status = stator->stator_fault();
  stator_status_tmp.id = atoi(stator->stator_id().c_str());
  if (check_status_history(stator_status_tmp))
  {
     //Callback for the API to pass data to the map
     _stator_callbacks->insert_zone_status(stator_status_tmp);  //Exception is thrown Here
  } }}

void Stator::set_callback(Motor_callbacks * _mtr_callback)
{_stator_callbacks = _mtr_callback;}

Not sure where I am going wrong here. What should I change in the code to get rid of that exception.

  • Off the top, `Assembly::prepare` exhibits undefined behavior by way of reaching a closing brace of a non-`void` function without encountering a `return` statement. So do `Station::prepare` and `Station::assemble` – Igor Tandetnik Jun 26 '21 at 14:54
  • The title of the post says there's a problem with `this->callbacks` expression; but the code shown doesn't contain such an expression. For further assistance, prepare a [mcve] and indicate exactly where your debugger points to as the origin of the problem. – Igor Tandetnik Jun 26 '21 at 14:58
  • @IgorTandetnik , If you see the update function in the Assembly Class. I am thrown that exception in the **_stator_callbacks->insert_zone_status(stator_status_tmp)** function. – Yash Mehta Jun 26 '21 at 17:55
  • 0xCDCDCDCD is the debugger's way of saying "uninitialized variable". Apparently, `_stator_callbacks` is not initialized. For further assistance, prepare a [mcve]. – Igor Tandetnik Jun 26 '21 at 17:58

0 Answers0