ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
You create Foo f(info)
as a local, and then it goes out of scope and is destructed. However, you're also capturing a reference to f
within your lambda, and when f
goes out of scope you now have a dangling reference.
You should hold onto all of your Foo
objects; maybe make them own their subscriber and then keep them in an array somewhere, instead of just holding onto the subscriber objects.
class Foo {
public:
Foo(ros::NodeHandle& nh, std::string topic, RosIntrospection::Parser& parser)
: topic_(topic)
, parser_(parser)
, subscriber_(nh.subscribe(topic, 10, &Foo::topicCallback, this))
{
}
void topicCallback(const topic_tools::ShapeShifter::ConstPtr& msg)
{
---EXAMPLE CODE---
cout << _topic << endl;
}
private:
std::string topic_;
RosIntrospection::Parser& parser_;
ros::Subscriber subscriber_;
};
main(){
---SOME CODE---
std::vector<Foo> foos;
for (auto &info : topics)
{
foos.emplace_back( nh, info["topic"], parser );
}
---SOME MORE CODE---
}