gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:04:13 27 4
gpt4 key购买 nike

如果 C++ 程序被 Python 脚本调用,您如何让 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

会检测到它。

最佳答案

你想使用:

  --trace-children=yes

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

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

关于python - 让 Valgrind 检测 Python 脚本 : 调用的 C++ 程序的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com