让Valgrind检测Python脚本调用的C ++程序的内存泄漏:

时间:2014-05-26 20:18:32

标签: python c++ memory-leaks valgrind

如果python脚本调用了C ++程序,那么如何让Valgrind检查C ++程序中的泄漏而不仅仅是脚本?例如,如果leak.cc包含以下代码

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

并编译为a.outcall_aout.py包含

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

然后通过

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

不会检测leak.cc中的内存泄漏,而是通过

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

会检测到它。

1 个答案:

答案 0 :(得分:2)

您想使用:

  --trace-children=yes
在你的valgrind命令行中

。或者,如果您不关心python脚本,您可以在脚本中使用valgrind启动子进程:

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