4

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?

3
  • 3
    I don't think so... Are you sure it's a memory leak and not just the GC not running yet? Commented Dec 5, 2012 at 18:09
  • Actually, I tried to add periodically manual GC to the code, but the memory still blows up quickly. Commented Dec 5, 2012 at 18:45
  • I don't see the swig link here. Am I missing something? Commented Dec 7, 2012 at 23:56

1 Answer 1

1

No that's fine, objects you get passed to your c method are only borrowed, i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.