0
$\begingroup$

Rosanswers logo

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;
     }
 pub.publish(msg);
     rate.sleep();
}
}
    

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/libthread_db.so.1".
[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;
(gdb) 

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

Thanks


Originally posted by DRos on ROS Answers with karma: 23 on 2018-07-24

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by DRos with karma: 23 on 2018-07-24

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by PeteBlackerThe3rd on 2018-07-24:
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)

Comment by DRos on 2018-07-24:
Thanks for the reply Pete.

Comment by stevemacenski on 2018-07-24:
Can you mark this as answered?

Comment by DRos on 2018-07-25:
Sure, sorry about that. All done.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.