Ask Your Question
0

Segmentation fault in publishing array message

asked 2014-08-01 17:23:38 -0500

Glen gravatar image

Hi

I'm trying to publish polygonal position as message. The publisher is make correct but "segmentation error" shows when I rosrun the publisher, saying "Segmentation fault (core dumped)".

int main(int argc,char** argv)
{
    ros::init(argc, argv, "line_formation");
    ros::NodeHandle na;
    geometry_msgs::Polygon destination;

    ros::Subscriber sub= na.subscribe<sensor_msgs::Joy>("/joy",1,joyCallback);
    ros::Publisher pub= na.advertise<geometry_msgs::Polygon>("uav_positions",10);
    ros::Rate loop_rate(2);

   while (ros::ok)
   {
      central_x +=array[4]; 
      central_y +=-array[3];
      central_z +=array[1];

      destination.points[0].x=central_x;
      destination.points[0].y=central_y;
      destination.points[0].z=central_z;

      pub.publish(destination);
      ros::spinOnce();
      loop_rate.sleep();
   }
}

As it shows, I use message "geometry_msgs/Polygon". It seems its caused by publishing array. If I publish single point message as "geometry_msgs/Point", it works. But I need to publish several points. So I have to publish array.

Any idea how to solve it or other ways to publish several points position?

Thanks Glen

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-08-01 17:38:53 -0500

ahendrix gravatar image

In C++ ROS messages, variable-sized arrays are implemented as a std::vector, which start with a size of 0. You're probably getting a segmentation fault because you're trying to use an array index that is out of bounds.

You should probably call resize() to set the size of the points array:

destination.points.resize(1);

If you're interested in learning more about the std::vector, my favorite reference is http://www.cplusplus.com/reference/ve...

edit flag offensive delete link more

Comments

It shows "geometry_msgs::Polygon has no member named 'resize' ". I'm nor sure is that because declaration of "geometry_msgs/Polygon". Still reading the reference you sent to me, trying to figure it out.

Glen gravatar imageGlen ( 2014-08-01 18:13:25 -0500 )edit

Sorry, it worked! I make a typing error when resize it. Thanks!

Glen gravatar imageGlen ( 2014-08-01 18:16:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-01 17:23:38 -0500

Seen: 1,087 times

Last updated: Aug 01 '14