Skip to content

Programming with YARP#

Programming in C++ with YARP#

Regarding close()#

If there exists a close method that needs to release unmanaged resources (dynamically allocated memory) or terminate stuff in an ordered manner (if using PolyDriver class members, e.g. close device A before device B), always define a class destructor that calls close, be it a DeviceDriver or an RFModule derived class. Also, make sure nothing bad happens if this close method is called several times (i.e. set dangling pointers to nullptr). Why is that:

  • PolyDriver::open may fail to initialize a subdevice, but it does not call the subdevice's close method; instead, it is immediately destructed via delete (ref).
  • If RFModule::configure returns false, as explained in the above comments, close will never be called, hence we also want to use a destructor here.
  • PolyDriver::close will never close a wrapped device twice, but callers of RFModule can do that inadvertently because of the previous point: once after a successful RFModule::configure and a CTRL+C signal (it just stops execution flow and calls close before leaving runModule), and one more time on class destruction.
  • Despite those two mechanisms being relatively different, this policy ensures we treat both RFModule and DeviceDriver constructs in a similar manner: close() on destruction, avoid dangling pointers.

If you have any doubts or comments#

Please read the Asking Questions section, and once you've succeded with its self-evaluation follow the recommendations by commenting publicly HERE if required.