Valgrind is a programming tool used for memory debugging. In the ros wiki (accessed Aug 2023), the use of using roslaunch with valgrind is described. This post will demonstrate the use of valgrind with a rosnode via roslaunch.
The results of this post was obtained on Ubuntu 20.04 with ROS Noetic.
Setup
First, you need to have valgrind installed. On Ubuntu, you can install valgrind with:
Roslaunch File
Using roslaunch, one simply needs to include valgrind in the launch-prefix tag for the node to be launched with valgrind. The segfault-example-node will be used as an example node to be launched.
Running
Launching the rosnode via roslaunch, in addition to the usual roslaunch printout, the following also appears:
This indicates that valgrind is indeed running with the target executable.
Debugging
In my case, I will induce a segfault. After a segfault was induced, the following can be found near the top of the printout:
Indeed, similar to the example in gdb with ros, it also points to the fact that line 24 was problematic. In particular, I was trying to access element 0 of a std::vector of length 0 and capacity 0.
Conclusion
In this post, I’ve demonstrated the simple use of valgrind with ROS, via integrating in via roslaunch. Valgrind is much more powerful than the example I’ve shown here, and it is often using for debugging memory issues and profiling. Indeed, if you suspect memory leak, you can start profiling memory leak by adding arguments to the launch-prefix like so: