2

我正在尝试追踪我使用 Boost.Python 在 C++ 中编写的 Python 扩展中的内存泄漏。我试图使用gperftools。但是,它似乎根本不能很好地与 Python 配合使用。

这是一个简单的例子,我要公开std::vector<int>

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

namespace py = boost::python;

std::vector<int> getIs(int n) {
    return std::vector<int>(n, 42);
}

BOOST_PYTHON_MODULE(Foo)
{
    py::class_<std::vector<int>>("IList")
        .def(py::vector_indexing_suite<std::vector<int>>() )
    ;   

    py::def("getIs", getIs);
}

如果我按照建议编译该模块-ltcmalloc,那么即使是简单的迭代也会崩溃:

>>> import Foo
>>> print [i for i in Foo.getIs(1)]
Segmentation fault (core dumped)

调用堆栈点在这里:

if (self.m_start == self.m_finish)
    stop_iteration_error(); // <==
return *self.m_start++;

我怀疑这是因为 gperftools 没有处理在 python 中一直抛出异常的事实,这是意料之中的。有没有人有使用 gperftools 来追踪这样的泄漏的经验?

这甚至可能吗,还是我基本上只是卡住了?

4

0 回答 0