Integrating check on memory leak into C code for autograding

Viewed 40

I want to integrate into my C code a flag that is e.g. 1 if a memory leak occurs, and 0 if not. I need this sort of functionality to auto-grade student assignments in a course I am teaching. I don't need to know where the memory leak is, just whether one occurs in the runtime of the executable. For informative tracing/debugging, Valgrind is my go-to, but I am reluctant to parse Valgrind text output for grading.

I have experimented with:

  • mcheck.h - mtrace() requires the use of an environment variable which gets a little hairy because of my autotesting setup (and getting a group of 300 first year students to run shell commands, which they have a weird resistance to)
  • malloc.h - mallinfo() on a memory-clean executable has the same information as when I run it for a particular memory-unclean executable
1 Answers

Check the return value of valgrind. By default valgrind returns whatever the process under test returns. But this can be changed to return a specific value if valgrind detects an error.

The valgrind manual explains that best:

--error-exitcode= [default: 0]

Specifies an alternative exit code to return if Valgrind reported any errors in the run. When set to the default value (zero), the return value from Valgrind will always be the return value of the process being simulated. When set to a nonzero value, that value is returned instead, if Valgrind detects any errors. This is useful for using Valgrind as part of an automated test suite, since it makes it easy to detect test cases for which Valgrind has reported errors, just by inspecting return codes.

Related