How can I use valgrind with Python C++ extensions?

让人想犯罪 __ 提交于 2019-11-27 03:27:38

Yes, you can use valgrind with Python. You just need to use the valgrind suppression file provided by the Python developers, so you don't get a bunch of false positives due to Python's custom memory allocation/reallocation functions.

The valgrind suppression file can be found here: http://svn.python.org/projects/python/trunk/Misc/valgrind-python.supp

IMPORTANT: You need to uncomment the lines for PyObject_Free and PyObject_Realloc in the suppression file*.

The recommended usage syntax is:

$ valgrind --tool=memcheck --suppressions=valgrind-python.supp \
                                          python -E -tt ./my_python_script.py

See also this README file from the Python SVN repo which describes the different ways of using Python with valgrind: http://svn.python.org/projects/python/trunk/Misc/README.valgrind

* - Alternatively, you can recompile Python with PyMalloc disabled, which allows you to catch more memory leaks that won't show up if you just suppress PyMalloc.

Kamil Kisiel

In Python 2.7 and 3.2 there is now a --with-valgrind compile-time flag that allows the Python interpreter to detect when it runs under valgrind and disables PyMalloc. This should allow you to more accurately monitor your memory allocations than otherwise, as PyMalloc just allocates memory in big chunks.

Yes you can: you do have a target to run valgrind with -- it's the python interpreter itself:

valgrind python foo.py

However, the results of above may not be very satisfactory -- Python is built in opt mode and with a special malloc, which may drown you in false positives.

You'll likely get better results by first building a debug version of Python. Start here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!