Rosanswers logo


I am trying to publish PCL point clouds which are obtained from velodyne point cloud. The program is compiled and running. I can even see the height and width information from the PCL point clouds but when I open RVIZ and visualize the published PCL point clouds, I get this message.

[ERROR] [1602489936.969819842]: Exception thrown when deserializing message of length [453372] from [/my_filter]: std::bad_alloc

ros::Publisher ptc_pub;
void callback(const ImageConstPtr& image, const PointCloud2ConstPtr& pt_cloud)
  pcl::PointCloud<pcl::PointXYZ> pcl_cloud;
  pcl::fromROSMsg(*pt_cloud, pcl_cloud);
  int PCLwidth = pcl_cloud.width;
  int PCLheight = pcl_cloud.height;
  cout<< "width = "<<PCLwidth <<endl;
  cout<< "height"<<PCLheight <<endl;
  // ros::spinOnce();
  // loop_rate.sleep();
int main(int argc, char** argv)
  ros::init(argc, argv, "my_filter");
  ros::NodeHandle node_;

  ros::Publisher ptc_pub = node_.advertise<PointCloud>("pcl_pts", 1);
  // ros::Rate loop_rate(10);

  message_filters::Subscriber<Image> img_sub(node_, "cam_1/color/image_raw", 1);
  message_filters::Subscriber<PointCloud2> pointcloud_sub(node_, "velodyne_points", 1);

  typedef sync_policies::ApproximateTime<Image, PointCloud2> MySyncPolicy;
  Synchronizer<MySyncPolicy> sync(MySyncPolicy(10),img_sub, pointcloud_sub);
  sync.registerCallback(boost::bind(&callback, _1,_2));
  return 0;

What I am doing wrong ? My guess is that I have placed the publisher wrong or is it something else ? I am new to ROS and cpp. Any suggestion will be really useful.


Originally posted by aravindsaiUR on ROS Answers with karma: 3 on 2020-10-12

Post score: 0


1 Answer 1


Rosanswers logo

As you guessed, the type of the variable you are giving to the publisher is different; it will convert to sensor_msgs::PointCloud instead of pcl::PointCloud, so try the following

void callback(const ImageConstPtr& image, const PointCloud2ConstPtr& pt_cloud)
  PointCloud pt_cloud1;
  convertPointCloud2ToPointCloud(*pt_cloud, pt_cloud1);

ref: convertPointCloud2ToPointCloud

Originally posted by miura with karma: 1908 on 2021-05-19

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by aravindsaiUR on 2021-05-19:
You're right. That was the issue. Thanks :)

Comment by miura on 2021-05-19:
You're welcome.


Your Answer

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