Skip to content

Memory leak on pyo3 object created within Rust #2853

@haixuanTao

Description

@haixuanTao

Bug Description

I think there is a memory leak when some args are used by a call_method1 in a Rust function that is being packaged and called from Python. Those args does not get removed until the very end of the function.

In my case, the args in question were PyBytes and PyDict. I do not know if it is specifically linked to this type.

To fix it, I have manually removed the reference with:

                unsafe {
                    ffi::Py_DECREF(bytes.as_ptr());
                    ffi::Py_DECREF(input_dict.as_ptr());
                }

See: dora-rs/dora#168
This fix was inspired by: #1801

Steps to Reproduce

You'll find a minimal reproducible example here: https://github.com/haixuanTao/pyo3_memory_leak

The expected behaviour is that the PyBytes get unallocated at each loop step.

Backtrace

No response

Your operating system and version

Linux Ubuntu 20.04

Your Python version (python --version)

Python 3.7.9

Your Rust version (rustc --version)

rustc 1.66.0 (69f9c33d7 2022-12-12)

Your PyO3 version

0.16

How did you install python? Did you use a virtualenv?

conda

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions