C++ 在c+中使用向量时出现分段错误+;
使用向量时出现分段错误似乎是一个常见问题,但我似乎仍然无法解决我的问题。我不清楚getArmPose函数代码段中的向量为什么会导致分段错误C++ 在c+中使用向量时出现分段错误+;,c++,vector,segmentation-fault,C++,Vector,Segmentation Fault,使用向量时出现分段错误似乎是一个常见问题,但我似乎仍然无法解决我的问题。我不清楚getArmPose函数代码段中的向量为什么会导致分段错误 class CytonServer { public: CytonServer(); private: std::vector <double> arm_pos_; .... void CytonServer::publish_Callback() { ROS_INFO("PUBLISH");
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_;
}
class-CytonServer
{
公众:
CytonServer();
私人:
标准::矢量臂位置;
....
void CytonServer::publish_Callback()
{
ROS_信息(“发布”);
boost::mutex::作用域锁定(publish\u mutex);
如果(遥控操作车=假)
{
ROS_信息(“发布”);
末端效应器\u type=“点末端效应器”;
//这是下面的函数
arm_pos_=cytonCommand.getArmPose();
.....
}
}
//在另一个文件中,调用了以下方法并导致分段错误
std::vector eccytonCommand::getArmPose()
{
双x,y,z;
标准::矢量臂位置;
ECManufactor解除影响或安置实际安置;
ECCoordinationSystemTransformation实际命令;
获取实际位置(实际位置);
ActualCord=ActualePlacement.offsetTransformations()[0].coordSysXForm();
臂位置向后推(实际命令平移().x());
手臂位置向后推(实际命令平移().y());
手臂位置向后推(实际命令平移().z());
返回臂位置;
}
对于如何解决此问题的任何帮助,我们将不胜感激。感谢您的快速反馈。我们发现问题不在于矢量,而在于我调用的第三方应用程序不是线程安全的。添加互斥锁已解决此问题
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;
}
std::vector EcCytonCommands::getArmPose()
{
boost::mutex::作用域锁定(publish\u mutex);
标准::矢量臂位置;
ECManufactor解除影响或安置实际安置;
ECCoordinationSystemTransformation实际命令;
获取实际位置(实际位置);
ActualCord=ActualePlacement.offsetTransformations()[0].coordSysXForm();
臂位置向后推(实际命令平移().x());
手臂位置向后推(实际命令平移().y());
手臂位置向后推(实际命令平移().z());
std::cout看起来该向量不应该引起本例中的任何问题。请尝试替换getArmPose()中的内容,使其不执行任何查询,然后将_向后推()一些测试值。查看该函数中是否仍然存在SEGFULTS。能否更具体地说明SEGFULTS发生的位置?使用valgrind之类的工具,它是检测内存泄漏、SEGFULTS、未初始化值等的绝佳工具。矢量SEGFULTS几乎总是来自不存在的索引的读取。不要使用std::vector
因为这个数字在编译时是已知的(而且对象很小),所以最好使用std::array
。translation()和x()的函数签名是什么?