bcdev / jpy

A bi-directional Python-Java bridge used to embed Java in CPython or the other way round.
Apache License 2.0
187 stars 37 forks source link

Memory Leak - new PyObject #170

Open prsvic opened 5 years ago

prsvic commented 5 years ago

Hello guys,

I noticed that when creating a PyObject in Java using method call(...) or callMethod(...), it lives forever....

In method PyLib_CallAndReturnObject, an object is first created (pyReturnValue = PyObject_CallObject), then its reference is incremented (Py_INCREF (pyReturnValue)), and then the reference is incremented again in the PyObject constructor in Java:

    PyObject(long pointer) {
        if (pointer == 0) {
            throw new IllegalArgumentException("pointer == 0");
        }
        PyLib.incRef(pointer);
        this.pointer = pointer;
    }

However, I found only one case, when the counter decrements - the PyObject's finalize(). Therefore, the object continues to exist with two references, and a memory leak occurs. Is this a bug, or am I misunderstanding something? (sorry, this is my first expirience with Python C API)