2014-04-11 54 views
0

看来,使用载体的情况越来越细分的故障是一个常见的问题,但我仍然不能似乎解决我的问题。我不清楚为什么getArmPose函数代码段中的矢量导致分段错误。分段故障++

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_; 
} 

有关如何解决此问题的任何帮助将不胜感激。

+0

它看起来不像是矢量应该引起这个例子的任何问题。尝试替换getArmPose()中的东西,不要执行任何查询,只需push_back()一些测试值。看看它是否仍然在该函数中发生段错误。 – qeadz

+2

你可以更具体地了解段错误发生的位置吗?使用像valgrind这样的工具,它是一个很好的工具来检测内存泄漏,段错误,未初始化的值等。 – example

+0

向量segfaults几乎总是来自不存在的索引读取。 –

回答

0

感谢您的快速反馈。我们发现问题不在于媒体,而是我所调用的第三方应用程序不是线程安全的。添加互斥锁已解决此问题。

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; 
}