-
Notifications
You must be signed in to change notification settings - Fork 760
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
Fix memory leak when calling Python from Rust #1806
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
The owned pointer isn't dereferenced
That's not what Py_DECREF
does (see https://docs.python.org/3/c-api/refcounting.html for more on refcounting).
What happened here was that the raw pointer never had its reference count decremented, so the inner value was leaked and never deallocated. It's similar to mem::forget
'ing a Rc
in Rust.
Thanks for the correction! Updated the comment on the PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! @davidhewitt needs cherry-pick to 0.14...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
auto-merge works! 🎉 |
Python::run_code
transforms the Rust code string to an owned pointer. The reference count to the owned pointer isn't decremented, which results in a memory leak when calling Python from Rust.This PR adds a missing
Py_DECREF
call on the owned pointer which fixes the memory leak.Fixes #1801.
Thanks to @mejrs for the tip on the fix.