简体   繁体   中英

Segmentation fault when using a vector in c++

It appears that getting segmentation faults when using a vector is a common issue, however I still can't seem to resolve my issue. It is not clear to me why the vector in the getArmPose function code segment is causing a segmentation fault.

class CytonServer
{
public:
    CytonServer();
private:
    std::vector <double> arm_pos_;
....


    void CytonServer::publish_Callback()
    { 
     ROS_INFO("PUBLISH");
      boost::mutex::scoped_lock lock(publish_mutex_);
      if (teleop_vehicle_==false)
      {
         ROS_INFO("Publishing ");
         end_effector_type = "point_end_effector"; 

         //Here is the culprip see function below
         arm_pos_ = cytonCommands.getArmPose();
         .....
       }
    }

//In a different file is the following method that is called and is causing the segmentation fault


std::vector <double> EcCytonCommands::getArmPose()
{

  double x,y,z;
  std::vector<double> arm_pos_;
    EcManipulatorEndEffectorPlacement actualEEPlacement;
    EcCoordinateSystemTransformation actualCoord;

    getActualPlacement(actualEEPlacement);
    actualCoord=actualEEPlacement.offsetTransformations()[0].coordSysXForm();

    arm_pos_.push_back(actualCoord.translation().x());
    arm_pos_.push_back(actualCoord.translation().y());

    arm_pos_.push_back(actualCoord.translation().z());

    return arm_pos_;
}

Any assistance on how to resolve this issues will be greatly appreciated.

Thanks for the quick feedback. We have discovered that the issue was not with the vectors but instead the 3rd party app I was calling was not thread safe. Adding a mutex lock has resolved the issue.

std::vector <double> EcCytonCommands::getArmPose()
{
  boost::mutex::scoped_lock lock(publish_mutex_);
  std::vector<double> arm_pos_;

    EcManipulatorEndEffectorPlacement actualEEPlacement;
    EcCoordinateSystemTransformation actualCoord;

    getActualPlacement(actualEEPlacement);
    actualCoord=actualEEPlacement.offsetTransformations()[0].coordSysXForm();

    arm_pos_.push_back(actualCoord.translation().x());
    arm_pos_.push_back(actualCoord.translation().y());

    arm_pos_.push_back(actualCoord.translation().z());

    std::cout << "size, x, y, z: " <<arm_pos_.size()<< " "<<arm_pos_[0] <<", "<<arm_pos_[1]<<", "<<arm_pos_[2] <<std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM