【问题标题】:segmentation fault (core dumped) ROS c++ arrow分段错误(核心转储) ROS c++ 箭头
【发布时间】:2018-03-01 22:05:32
【问题描述】:

我正在尝试读取 geometry_msgs::PoseArray 并将其转换为用于 ROS 的 c++ 中的 geometry_msgs::Pose。它应该只在主题/tag_detections_pose 上有数据时转换数据。当我运行这个节点(见代码)时,当 /tag_detections_pose 上有一个空数组时,我在标记的位置得到一个分段错误(核心转储)。我希望这是指针的问题,但找不到确切的位置。我想我必须初始化我的指针,但我不知道具体在哪里。 (b.t.w. 代码是将apriltag_ros消息转换为simulink可读)

#include "ros/ros.h"
#include <geometry_msgs/Pose.h>
#include <geometry_msgs/PoseArray.h>
#include <std_msgs/Bool.h>
#include <array>


class SubscribeAndPublish
{
public:
  SubscribeAndPublish()
  {
    //Topic you want to publish
    pub_ = n_.advertise<geometry_msgs::Pose>("/tag_pose", 1);

    //Topic you want to subscribe
    sub_ = n_.subscribe<geometry_msgs::PoseArray>("/tag_detections_pose", 1, &SubscribeAndPublish::callback, this);
  }

  void callback(const geometry_msgs::PoseArray::ConstPtr& input)
  {
    if (input) {
      geometry_msgs::Pose output;

      output.position.x = input->poses[0].position.x; //Here I get the segmentation fault
      output.position.y = input->poses[0].position.y;
      output.position.z = input->poses[0].position.z;
      output.orientation.x = input->poses[0].orientation.x;
      output.orientation.y = input->poses[0].orientation.y;
      output.orientation.z = input->poses[0].orientation.z;
      output.orientation.w = input->poses[0].orientation.w;
      pub_.publish(output);
    }

  }

private:
  ros::NodeHandle n_;
  ros::Publisher pub_;
  ros::Subscriber sub_;

};//End of class SubscribeAndPublish

int main(int argc, char **argv)
{
  //Initiate ROS
  ros::init(argc, argv, "apriltag_to_simu_V2");

  SubscribeAndPublish SAPObject;

  ros::spin();

  return 0;
}

编辑:一些额外的信息。 每当我从 /tag_detections_pose 读取下一个数据时,就会发生分段错误。

header: 
  seq: 14192
  stamp: 
    secs: 1519941974
    nsecs: 650975624
  frame_id: usb_cam
poses: []
---
header: 
  seq: 14193
  stamp: 
    secs: 1519941974
    nsecs: 995796728
  frame_id: usb_cam
poses: []
---

每当我看到 apriltag 时,都没有分段错误。那么下面的代码来自/tag_detections_pose:

---
header: 
  seq: 80
  stamp: 
    secs: 1519981664
    nsecs: 100302767
  frame_id: usb_cam
poses: 
  - 
    position: 
      x: 0.110443956833
      y: -0.080843233518
      z: 0.753152633488
    orientation: 
      x: 0.706039345202
      y: 0.705726892216
      z: -0.0580458686961
      w: 0.00941667438916
---
header: 
  seq: 81
  stamp: 
    secs: 1519981664
    nsecs: 368606478
  frame_id: usb_cam
poses: 
  - 
    position: 
      x: 0.110435802307
      y: -0.0808251086937
      z: 0.753089835655
    orientation: 
      x: 0.706068882885
      y: 0.705719138117
      z: -0.0577643573596
      w: 0.0095136604333
---

【问题讨论】:

  • 在哪一行出现段错误?使用调试器运行。
  • @DBedrenko 跟评论//Here I get the segmentation fault就行了
  • 你怎么知道poses的第一个元素确实存在?

标签: c++ pointers segmentation-fault simulink ros


【解决方案1】:

您应该检查您真正关心的内容,而不是检查if (input):poses 字段是否为非空。

在 ROS 消息中,数组存储为 C++ vectors。要检查poses 向量是否有任何元素,您可以将您的条件替换为if (!input-&gt;poses.empty())

【讨论】:

    【解决方案2】:

    可能seg fault 发生原因您在订阅者构造函数中声明了非指针类型(我还没有测试过这个)。

    你不必初始化 geometry_msgs::PoseArray::ConstPtr&amp; 你可以只做 geometry_msgs::PoseArray 并摆脱指针

    【讨论】:

      【解决方案3】:

      我是这样解决这个问题的。 在data = msg-&gt;detections[0].pose.pose;之前添加if(msg-&gt;detections.size()&gt;0)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 2017-02-25
        • 2016-07-12
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多