I created an executable that contains a ROS2(foxy) node. When I run the executable everything work and I have no errors.
When I run it with valgrind using the following command:
timeout --preserve-status 20s valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=../Docs/reports/dynamic_checker/report_valgrind.txt ./bin/Linux/my_app
I had the following error:
[ERROR] [1656072705.908080979] [rclcpp]: failed to finalize rcl init options: init_options->impl argument is null, at /tmp/binarydeb/ros-foxy-rcl-1.1.13/src/rcl/init_options.c:133
I discovered that it was caused by the calling of the shutdown() method at the wrong moment. I fix it by manually calling the shutdown before the program's exit:
if (rclcpp::ok())
{
rclcpp::shutdown();
}
ros_node_ptr.reset();
This solved the previous error but now I have a memory leak:
==713705== 144 (56 direct, 88 indirect) bytes in 1 blocks are definitely lost in loss record 30 of 40
==713705== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==713705== by 0x4A46892: ??? (in /opt/ros/foxy/lib/librcl.so)
==713705== by 0x4918949: rclcpp::Context::get_interrupt_guard_condition(rcl_wait_set_t*) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x492EC4A: rclcpp::graph_listener::GraphListener::GraphListener(std::shared_ptr<rclcpp::Context>) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x4952E0B: std::shared_ptr<rclcpp::graph_listener::GraphListener> rclcpp::Context::get_sub_context<rclcpp::graph_listener::GraphListener, std::shared_ptr<rclcpp::Context> >(std::shared_ptr<rclcpp::Context>&&) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x4950313: rclcpp::node_interfaces::NodeGraph::NodeGraph(rclcpp::node_interfaces::NodeBaseInterface*) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x4942E6B: rclcpp::Node::Node(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, rclcpp::NodeOptions const&) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x49443F7: rclcpp::Node::Node(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, rclcpp::NodeOptions const&) (in /opt/ros/foxy/lib/librclcpp.so)
==713705== by 0x19A51A: ROS2Interface::ROS2CommunicationNode::ROS2CommunicationNode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (ROS2CommunicationNode.cpp:29)
==713705== by 0x197B3A: construct<ROS2Interface::ROS2CommunicationNode> (new:174)
==713705== by 0x197B3A: construct<ROS2Interface::ROS2CommunicationNode> (alloc_traits.h:483)
==713705== by 0x197B3A: _Sp_counted_ptr_inplace<> (shared_ptr_base.h:548)
==713705== by 0x197B3A: __shared_count<ROS2Interface::ROS2CommunicationNode, std::allocator<ROS2Interface::ROS2CommunicationNode> > (shared_ptr_base.h:679)
==713705== by 0x197B3A: __shared_ptr<std::allocator<ROS2Interface::ROS2CommunicationNode> > (shared_ptr_base.h:1344)
==713705== by 0x197B3A: shared_ptr<std::allocator<ROS2Interface::ROS2CommunicationNode> > (shared_ptr.h:359)
==713705== by 0x197B3A: allocate_shared<ROS2Interface::ROS2CommunicationNode, std::allocator<ROS2Interface::ROS2CommunicationNode> > (shared_ptr.h:702)
==713705== by 0x197B3A: make_shared<ROS2Interface::ROS2CommunicationNode> (shared_ptr.h:718)
==713705== by 0x14C575: main (Main.cpp:72)
To be sure I'm not creating the leak, I commented all the code inside ROS2CommunicationNode.
What can cause it?