Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for memory leak when using lambdas #1301

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

yeganer
Copy link

@yeganer yeganer commented Feb 27, 2018

Using a function as an argument to a C++ object causes a reference cycle
that the garbage collector is somehow not able to break, causing the
corresponding C++ object to leak.

Using a function as an argument to a C++ object causes a reference cycle
that the garbage collector is somehow not able to break, causing the
corresponding C++ object to leak.
yeganer added 2 commits March 14, 2018 12:39
Use a local dictionary instead of py::globals(). This should still cause the test to fail.
@rwgk
Copy link
Collaborator

rwgk commented Aug 30, 2021

I'd say there is a leak somewhere, but where?

I'd expect that local is automatically cleared when the C++ local variable goes out of scope, unless the local(s) passed via py::exec is/are kept alive elsewhere.

We know with a high degree of certainty that py::dict doesn't leak. Something that could easily be demonstrated by removing the py::exec call.

General: I do know that Py_Initialize and Py_Finalize should only be called once each in a process:

https://github.com/rwgk/stuff/blob/dc76a8713a2fdaf520893d96255da56fe8a401ab/noddy/embedded_noddy_main_run.c

Python does not fully clean up after itself. I believe it could maybe be better than it is, but a full cleanup seems infeasible. See "Bugs and caveats" here:

https://docs.python.org/3/c-api/init.html#c.Py_FinalizeEx

One way to get more certainty about the leak would be to reduce py::exec to the raw Python C API equivalent (seems pretty straightforward, if someone wants to try). The outcome could well be that pybind11 is fine and that the leak is in Python. Or not, idk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants