Ask Your Question

Segmentation fault whilst trying to publish PointCloud

asked 2018-07-24 07:48:49 -0500

DRos gravatar image

Hi all,

I'm trying to publish a PointCloud so that I can visualise it in RViz, however even though my node will compile I get a segmentation fault when I run it.

#include <ros/ros.h>
#include <stdlib.h>
#include <sensor_msgs/PointCloud.h>

int main(int argc, char **argv){
        // Initialise ROS and set up a node
        ros::init(argc, argv, "send_numbers");
        ros::NodeHandle nh;

    //Creates the publisher, and tells it to publish to a topic
ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud>("number", 5);
//initialise random function
srand (time(0));

//Sets the loop to publish at a rate of 1Hz
ros::Rate rate(1);

while(ros::ok()) {
 sensor_msgs::PointCloud msg;
     msg.header.stamp = ros::Time::now();
 msg.header.frame_id = "/map";
 for(unsigned int i  =  0; i < 100; ++i){
 msg.points[i].x = rand() % 10;
     msg.points[i].y = rand() % 10;
 msg.points[i].z = rand() % 2;

This is my gdb output

   Reading symbols from devel/lib/number_output/number_output...done.
(gdb) r
Starting program: /home/dan/catkin_ws/devel/lib/number_output/number_output 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/".
[New Thread 0x7ffff1855700 (LWP 21659)]
[New Thread 0x7fffe9054700 (LWP 21660)]

New Thread 0x7ffff1054700 (LWP 21661)]
[New Thread 0x7ffff0853700 (LWP 21666)]

Thread 1 "number_output" received signal SIGSEGV, Segmentation fault.
0x000000000040cdb0 in main (argc=1, argv=0x7fffffffdaa8)
    at /home/dan/catkin_ws/src/number_output/src/send_numbers.cpp:25
25       msg.points[i].x = rand() % 10;

Sorry for the funny format. I'm not sure what happened there. I'm new to ROS so if any help would be appreciated.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-07-24 09:06:22 -0500

DRos gravatar image

updated 2018-07-24 09:32:42 -0500

I think I worked it out, the msg.points array doesn't have a size allocated before assigning values to it. I had to put msg.points.resize(num_points) to assign it the correct size before assignment.

Thanks for looking! Dan

edit flag offensive delete link more



You are correct. The points member is a std::vector object you can either set the size then add the data (fastest) or you can use the push_back method to add elements one by one (slower)

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-07-24 09:09:41 -0500 )edit

Thanks for the reply Pete.

DRos gravatar image DRos  ( 2018-07-24 09:14:46 -0500 )edit

Can you mark this as answered?

stevemacenski gravatar image stevemacenski  ( 2018-07-24 19:24:13 -0500 )edit

Sure, sorry about that. All done.

DRos gravatar image DRos  ( 2018-07-25 01:58:40 -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


Asked: 2018-07-24 07:48:49 -0500

Seen: 139 times

Last updated: Jul 24 '18