Memory leak in Python-C ++ interface?

I have a piece of C ++ code with a python-C ++ interface that needs to be called multiple times using the python list as its input. I even found a fictitious process, as the following leads to a memory leak:

In python:

a = [1.0]*1000 for c in range(1000): dummy(a, 1) 

In C ++:

 static PyObject* dummy(PyObject* self, PyObject* args) { Py_RETURN_NONE; } 

Is something missing for me, so it introduces a memory leak?

+4
source share
1 answer

No, that’s fine, the objects that you pass to your c-method are borrowed, i.e. you do not need to reduce the number of returned objects before returning (in fact, this will be a bad, bad error).

See, for example, this part of the document:

Please note that any links to Python objects provided by the subscriber are borrowed links; Do not reduce their link count!

How do you even determine that you have a memory leak? It is more than likely your problem.

+1
source

All Articles