Ask Your Question

Revision history [back]

Callback not updating class member variable

I have encountered a strange problem. I have a standard class

class A{
 A(ros::NodeHandle& nh){
        sub = nh.subscribe("cmd", 10, &A::cmd_callback, this);
         cmd_received = "none";
 void cmd_callback(const std_msgs::String::ConstPtr& msg){
        cmd_received = msg->data;
        std::cout<< "cmd received in callback: " << cmd_received << std::endl;
 void update(){
   ROS_INFO_STREAM_THROTTLE(2.0, "cmd received: " << cmd_received);
   std::string cmd_received;
   ros::Subscriber sub

Now when I send a string message from my python publisher it seems to be correctly received. The print statement in the callback outputs the correct value. However when calling the update function in the main loop, the value of cmd_received is the initial one ("none").

This is seems to be a very subtle bug, normally this kind of structure works great.

Any suggestions are appreciated.