简体   繁体   中英

Memory leak in Python-C++ interface?

I have a piece of C++ code with python-C++ interface that need to be called repeatedly with a python list as its input. I found even the dummy process as following leads to 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;
}

Am I miss anything here so it introduces memory leak?

No that's fine, objects you get passed to your c method are only borrowed, ie you don't have to decrease the refcount of the objects before returning (as a matter of fact that would be a bad, bad bug).

See for example this part of the documentation :

Note that any Python object references which are provided to the caller are borrowed references; do not decrement their reference count!

How are you even determining that you have a memory leak? It's more than likely that that's your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM