How to perform an action at nodelet unload / shutdown?
I'm trying to print / publish some statistics / diagnostic info at the time of nodelet unloading / exit. According to this answer on ros-users by Josh Faust the destructor of the nodelet should be used for this.
The problem I'm experiencing is that the text is sometimes printed, sometimes not at all and sometimes I get:
...
[my_nodelet_mgr-2] killing on exit
pure virtual method called
terminate called without an active exception
[rosout-1] killing on exit
[master] killing on exit
...
The nodelet class basically wraps another class with one publisher and one subscriber that does all the work, which is instantiated in MyNodelet::onInit()
. Nothing sophisticated. ~MyNodelet()
then prints the statistics using the NODELET_*
/ ROS_*
macros after retrieving them from the worker class.
Is the nodelet destructor the proper place to be doing things like this (lacking a nodelet::onExit()
or similar)? Any other ideas on how I could implement something like this?
how-to-catch-nodelet-shutdown-signal is related, but it explicitly deals with threading, which I'm not doing.
PS: Could it be that rosout
doesn't get the chance to flush it buffers (or something similar)? Even a single NODELET_INFO(..)
is printed only some of the time.
Did adding a sleep as Jack suggested caused ROS messages to be reliably printed? I tried sleeping for 1 second and still get intermittent results. I wonder if there is a way to explicitly flush rosout...