Reputation: 6338
I am facing a weird problem, which is related to arrays in C++. Basically, I made two instances of a class, and later I used these instances, which caused memory corruption error. The code works if I make individual two instances of this class without using array at all.
Please see the code snippet below-
int main(int argc, char** argv)
{
ros::init(argc, argv, "my_node");
ros::NodeHandle nh("~");
// this doesn't work
PointCloudSubscriber pcs[2];
pcs[0] = PointCloudSubscriber(nh, "/kinect1/sd/points", 1);
pcs[1] = PointCloudSubscriber(nh, "/kinect2/sd/points", 1);
// this works
//PointCloudSubscriber pc1(nh, "/kinect1/sd/points", 1);
//PointCloudSubscriber pc2(nh, "/kinect2/sd/points", 1);
ros::Rate loop_rate(10);
while (ros::ok())
{
// this doesn't work
for (size_t i = 0; i < 2; i++)
ROS_INFO_STREAM("Cloud topic=" << pcs[i].topic <<
", size=" << pcs[i].point_cloud.data.size());
// this works
//ROS_INFO_STREAM("Cloud topic=" << pc1.topic <<
// ", size=" << pc1.point_cloud.data.size());
//ROS_INFO_STREAM("Cloud topic=" << pc2.topic <<
// ", size=" << pc2.point_cloud.data.size());
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
class PointCloudSubscriber
{
private:
ros::Subscriber subscriber;
void callback(
const sensor_msgs::PointCloud2ConstPtr& msg);
public:
std::string topic;
sensor_msgs::PointCloud2 point_cloud;
PointCloudSubscriber(){};
PointCloudSubscriber(
ros::NodeHandle& node_handle,
std::string topic_name,
int queue_size);
};
void PointCloudSubscriber::callback(
const sensor_msgs::PointCloud2ConstPtr& msg)
{
point_cloud = *msg;
}
PointCloudSubscriber::PointCloudSubscriber(
ros::NodeHandle& node_handle,
std::string topic_name,
int queue_size)
{
topic = topic_name;
subscriber = node_handle.subscribe<sensor_msgs::PointCloud2>(
topic_name, queue_size, &PointCloudSubscriber::callback, this);
}
Below is the output reported-
[ INFO] [1526440334.856181149]: Cloud topic=/kinect1/sd/points, size=0
[ INFO] [1526440334.856210465]: Cloud topic=/kinect2/sd/points, size=0
*** Error in `/home/ravi/ros_ws/devel/lib/my_pcl_tutorial/check': double free or corruption (!prev): 0x000000000128f220 ***
Aborted (core dumped)
The interesting thing to note here is that each element of the array is able to get the correct topic
but somehow fails to get the point_cloud
attribute which is assigned from boost::shared_ptr
.
Why such strange behavior? Is it an illegal use case of accessing boost::shared_ptr
? Any suggestions, please?
PS: I am using ROS Indigo on Ubuntu 14.04 LTS 64 bit PC.
Upvotes: 1
Views: 95
Reputation: 40150
PointCloudSubscriber
is probably broken1. Instead of creating two default instances (1)2 of PointCloudSubscriber
and then assign them the "real" values (2)2, do:
PointCloudSubscriber pcs[2] = {
PointCloudSubscriber(nh, "/kinect1/sd/points", 1),
PointCloudSubscriber(nh, "/kinect2/sd/points", 1)
};
1) It looks like it handles resources, but only defines a user-provided constructor. You should observe the rule of 0/3/5.
2) FYI, (1) and (2):
PointCloudSubscriber pcs[2]; // (1)
pcs[0] = PointCloudSubscriber(nh, "/kinect1/sd/points", 1); // (2)
pcs[1] = PointCloudSubscriber(nh, "/kinect2/sd/points", 1); // (2)
Upvotes: 3