Ceedling
Ceedling copied to clipboard
How to test memory leak with Ceedling?
I searched the documentation and examples, but I couldn't find anything that tells me how I can test for memory leaks with Ceedling.
I know that in several posts @mvandervoord said that it is not a good idea to test the memory leak with unit tests, but in any case, I would like to see how I can detect a possible memory leak in my code.
One of the best approaches is not to unit test memory leaks, per se, but to use unit tests to exercise instrumented memory-manipulation code. The Valgrind toolset is your friend here. There is an open PR for a Valgrind Ceedling plugin, but it will be a while until this is incorporated into Ceedling (we're hard at work at pushing out a major release in version 0.32). However, you should be able to use Valgrind pretty easily without the plugin.
- Install Valgrind
- Write unit tests exercising memory-manipulating code
- Modify your Ceedling project file to include the
-g
debug flag for compilation and linking (assuming you're using the default GCC tools) - Use Ceedling to generate and build your test executables
- Run Valgrind directly with the individual test executables of interest from your project build directory
- Inspect Valgrind results (and potentially run a debugger with your test executables)
I managed to install and use Valgrind by following this guide https://blog.zaleos.net/unit-testing-c-code-with-ceedling-and-cmock/, although I don't like all the noise it makes in the output , even if there is no memory leak but only tests that fail Valgrind generates output with several exceptions.
Even though I didn't add the -g
flag it seems that Valgrind can detect memory leaks, I wonder if it really matters? I also didn't understand how to add this flag in the project.yml file
-g
is a flag given to the compiler to include debug symbols in the final program, instead of discard them.
Setting this flags should be under :tools:
where you've defined gcc
as your compiler (which is actually the default).
Regarding the memory leak output, that could be related to an older ceedling version or dockerfile base image which is old and leaks, and not necessarily regarding your tests.
@parmi93 Unfortunately, Valgrind's reports are Valgrind's reports. As you are well aware memory management can be complicated and so can be reporting on it.
Memory leak testing is well outside the Ceedling test suite of tools' abilities. It would require hooks into the C runtime's heap management. Valgrind really is one of the best options for finding memory problems. I've used it successfully a couple times to find quite tricky problems.
The reports will probably be rather unhelpful without debugging symbols built into your tests. Without those symbols Valgrind can't refer to variables, arrays, pointers, etc. in your code. When it has that information it can tell you when memory isn't freed, why null references are happening, that accesses are off the end of an array, etc.
I mentioned adding the -g
option to your test build to include debugging symbols. You aren't aware of how to do that in Ceedling. Sorry. I'll explain. This all assumes that you're relying on the built-in tool configurations using gcc
. Ceedling has an entire scheme for specifying tool command lines. If you don't need to modify the defaults, you'll never need to bother with that. Sometimes you only need to tweak a command line a little bit. In the version of Ceedling you're using, there's a limited but useful shortcut for modifying tool command lines. In your case, you'll just want to add the following to your project file. This adds the -g
to the end of the default compilation and linking command lines for your tests. If you'd like to see what Ceedling is running for command lines, you can turn up the verbosity (ceedling verbosity[4] test:all
).
:tools_test_compiler:
:arguments:
- -g
:tools_test_linker:
:arguments:
- -g
I use
sudo valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --log-file=memcheck.txt -v --error-limit=no ./build/test/out/test_XXX.out
A Valgrind plugin is planned to be added to Ceedling after 1.0.0 is released. A community member has already submitted an implementation that will likely be the base of this work (which should make it faster to crank out!).