2

I am trying to track down a memory leak in a Python extension I wrote in C++ using Boost.Python. I was trying to use gperftools. However, it appears that it does not play nicely with Python at all.

Here is a simple example, I'm exposing 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);
}

If I compile that module with -ltcmalloc as recommended, then even a simple iteration crashes:

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

The call stack points here:

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

I suspect this is because gperftools does not handle the fact that in python, exceptions are thrown all the time and that's expected. Does anybody have experience using gperftools to track down leaks like this?

Is this even possible, or am I basically just stuck?

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Based on your code, I wouldn't expect any memory leaks, since you're not returning any self-managed memory. I think the problem you're having in your example deals with the fact that you need to wrap std::vector in boost::python before assuming any functionality in your extension – WakkaDojo Apr 30 '15 at 16:37

0 Answers0