ROS2:当我运行valgrind时,我会由于节点创建而获得内存泄漏。什么会导致它?

发布于 2025-02-10 04:32:12 字数 3625 浏览 3 评论 0原文

我创建了一个包含ROS2(Foxy)节点的可执行文件。当我运行可执行文件时,所有内容都可以正常工作,但我没有错误。

当我使用以下命令使用valgrind运行它时:

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

我有以下错误:

[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

我发现它是由于错误时刻的shutdown()方法引起的。我通过在程序退出之前手动调用关闭来修复它:

if (rclcpp::ok())
{
      rclcpp::shutdown();
}
ros_node_ptr.reset();

这解决了以前的错误,但是现在我有一个内存泄漏:

==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)

确保我没有创建泄漏,我在ROS2 -CommunicationNode中评论了所有代码。

什么会导致它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文