0
$\begingroup$

Rosanswers logo

I have a code where I am checking for shutdown interrupt in a statement like so:

         int main()
          {
              bool  running(false);
              //running is set to true by a condition statement somewhere here
             for(; running || ros::ok() || !viewer->wasStopped() ;)
              {
               // ...             ...                          ...                  ...
               //do something
              }
          ros::shutdown();
          return 0;
       }

However, during runtime, when I do Ctrl + C, I get the usual warning ^C[ WARN] [1458383451.748234940]: Shutdown Signal Received. Shutting down ROS! but the node never shuts down.

Could I be missing something?


Originally posted by lakehanne on ROS Answers with karma: 152 on 2016-03-19

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Crlt+C will be handled by the ros node logic internal and causes ros::ok() to no longer return true but false . After ros:ok() is not true any more there is no need to call ros::shutdown()

I think your code may look like this

 int main()
  {
      bool  running(false);
      //running is set to true by a condition statement somewhere here

      // exit the main loop 
      //              if running flag is set false, 
      //          or if ros::ok() is not ok anymore (likely we received Crlt+C), 
      //          or if the view was stopped
     for(; running && ros::ok() && !viewer->wasStopped() ;)
      {
       // ...             ...                          ...                  ...
       //do something
      }
  
  if ( ros::ok() )
  {
      // we exited the main loop because of false running value or the viewer was stopped,
      // no Crlt+C received, i. e. we have to ros::shutdown() explizitly
      ros::shutdown();
  }
  else
  {
      // nothing to be done here, ros::ok() no longer true, ros::shutdown not needed anymore
  }
  return 0;

}


Originally posted by Wolf with karma: 7555 on 2016-03-19

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.