Segmentation fault whilst trying to publish PointCloud
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