ros2_controllers icon indicating copy to clipboard operation
ros2_controllers copied to clipboard

Use smart pointer instead of raw pointer

Open youtalk opened this issue 3 years ago • 1 comments

Modern C++ is preferred to use smart pointer instead of raw pointer for safer programming. But the current implementation is written using raw pointers e.g.:

  • https://github.com/ros-controls/ros2_control/blob/master/hardware_interface/include/hardware_interface/handle.hpp#L73
  • https://github.com/ros-controls/ros2_control/blob/master/hardware_interface/include/hardware_interface/robot_hardware.hpp#L136-L138

Why don't we use the shared_ptr and weak_ptr instead of the raw pointers? Do you have any real time related problem?

youtalk avatar Sep 04 '20 08:09 youtalk

It may be worthwhile to also investigate whether passing pointers at all is necessary/desirable.

Afaik, pointers (smart or not) should only be passed if resource sharing or transfer of ownership is the intent. If there is no such intent, perhaps a plain (const) reference would actually be better.

R.30: Take smart pointers as parameters only to explicitly express lifetime semantics.

gavanderhoorn avatar Sep 04 '20 08:09 gavanderhoorn