Are there potential pitfalls in ConstPtr & in a callback?

asked 2021-02-21 22:56:51 -0500

KenYN gravatar image

I'm looking at some code that does something unexpected, and I was just struck by a fundamental question. Given a callback like this:

void MyClass::MyCallback(const geometry_msgs::PoseStamped::ConstPtr &msg)
{
}

This is a queue that is being posted to every 20 milliseconds, but we have an ample 1000 item subscribe queue, although there are times when the nodelet is busy doing other stuff.

Now, if I dump the msg->header.stamp and compare it with the original value in the Bag file I am playing back, I see occasionally the timestamp in the callback is about 10 milliseconds later than the original timestamp, although the rest of the message seems OK.

I just checked our whole source tree, and my team's code always uses just ConstPtr msg, but another team frequently uses ConstPtr &msg.

Since ConstPtr is just a typedef for a smart pointer, and StackOverflow says references to smart pointers are bad, have I just discovered one reason why?

(NB: I seem to have answered my own question, but this seems like a pitfall other ROS users might encounter, and I cannot find a dup)

edit retag flag offensive close merge delete