Skip to content

Potential memory leak in using pyo3 with raw pointers *mut ffi::PyObject #2804

Answered by twitu
twitu asked this question in Questions
Discussion options

You must be logged in to vote

The PyO3 code looks fine.

I'm not a Cython expert, however I just had a quick read of https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html?highlight=cast#type-casting

What's your definition of uuid4_to_pystr that Cython sees? It looks like <str>uuid4_to_pystr(&self._mem) might be the cause? It sounds like this will increase the reference count by one, which will cause a leak?

Yup thanks for this pointer (pun 😛) the fix is to decrement the reference count after casting. Because there is no good way to cast without incrementing ref count.

cdef inline str pyobj_to_str(PyObject* ptr):
    cdef PyObject* str_obj = ptr
    cdef str str_value = <str> str_obj
    Py_XDECREF(

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@birkenfeld
Comment options

@twitu
Comment options

Comment options

You must be logged in to vote
3 replies
@davidhewitt
Comment options

@twitu
Comment options

@davidhewitt
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by twitu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants