user14717
user14717

Reputation: 5161

Getting Valgrind to detect memory leaks from C++ program called by Python Script:

If a C++ program is called by a python script, how do you get Valgrind to check leaks in the C++ program and not just in the script? For example, if leak.cc contains the following code

int main() {
    int* p = new int;
}

and is compiled into a.out, and call_aout.py contains

#!/usr/bin/env python
import subprocess
subprocess.call(["./a.out"])

then running valgrind via

valgrind --track-origins=yes --leak-check=full -v ./call_aout.py

will not detect the memory leak in leak.cc, but calling it via

valgrind --track-origins=yes --leak-check=full -v ./a.out

will detect it.

Upvotes: 1

Views: 1746

Answers (1)

Nick Bastin
Nick Bastin

Reputation: 31319

You want to use:

  --trace-children=yes

in your valgrind command line. Alternatively if you don't care about the python script all you can launch your subprocess with valgrind from within the script:

subprocess.call("valgrind --track-origins=yes --leak-check=full -v ./a.out")

Upvotes: 4

Related Questions