Does ros::Subscriber shutdown() block if there is an active callback?
I have tried searching the source code but it seems to be hidden deep inside TopicManager:
If a function callback of a ros::Subscriber is being executed, but the subscriber is shutdown()
or goes out of scope, will we get undefined behavior/crash?
I was hoping that shutdown()
would block till callback execution finishes.
This also makes me think that the any data living inside the callback should outlive the ros::Subscriber
(which would mean that the subscriber should be defined LAST, AFTER any other data (like in a class definition that contains subscriber and data)!
E.g.
struct A {
B data; // correct!
ros::NodeHandle n;
ros::Subscriber r;
//B data; // wrong!!! Will die earlier than subscriber, and if class instance
// dies while callback execution, we'll get a crash
A() {
boost::function<void(const MsgConstPtr &msg)> func =
[this](const MsgConstPtr &msg) {
this->data.DoStuffWith();
};
n.subscribe("topic", 10, func);
}
};