ros / ros_comm

ROS communications-related packages, including core client libraries (roscpp, rospy, roslisp) and graph introspection tools (rostopic, rosnode, rosservice, rosparam).
http://wiki.ros.org/ros_comm
747 stars 914 forks source link

Segmentation fault when advertising a topic #2300

Closed Risbo6 closed 1 year ago

Risbo6 commented 1 year ago

I'm facing a rather strange issue, in some parts of my code, advertising a topic led to a segfault.

In:

class RosPlatform {
    public:
        RosPlatform() {
            int argc = 0;
            ros::init(argc, nullptr, "platform", ros::init_options::NoSigintHandler);
            ros::NodeHandle n;
            ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
            ros::Rate loop_rate(10);

            int count = 0;
            while (ros::ok())
            {
                std_msgs::String msg;
                std::stringstream ss;
                ss << "hello world " << count;
                msg.data = ss.str();
                ROS_INFO("%s", msg.data.c_str());
                chatter_pub.publish(msg);
                ros::spinOnce();

                loop_rate.sleep();
                ++count;
            }
        }
}; 

This: ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);

Step over to:

  template <class M>
  Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)
  {
    AdvertiseOptions ops;
    ops.template init<M>(topic, queue_size);
    ops.latch = latch;
    return advertise(ops);
  }

Where return advertise(ops); creates a segmentation fault. Any idea what can cause this?

Risbo6 commented 1 year ago

Bug was caused by the usage of #pragma pack(1) in another file.