2014-04-11 56 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; 
}