-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[BUG] Memory leaks when casting raw pointers to python types #2742
Comments
Sososo, as far as I can see, this is because the Similar for |
In total, pybind11's tests leak about 5MB in total. The https://github.com/pybind/pybind11/blob/master/tests/test_copy_move.cpp#L217 |
So, current thoughts:
|
Note that
That's something you and I understand. For most users it's a very nice automagic conversion that just works and does what the user wants. I doubt a lot of users actually understand the distinction you're drawing here.
That is a big problem. My idea would be an extra argument for passing on a deleter. If no deleter was passed, keep doing what we're doing now.
Yup... Deleter argument, anyone? |
Same story as
Yeah, OK, but .. well, it is important to know, also in cases where you take that argument.
That also won't fix existing code, though. How is this different from e.g. adding a note to the docs, and offering "wrap it in a |
Yeah, just making sure it's mentioned for others to read it.
That's a good point. I could imagine a way that could save a few instructions with my proposal, but I agree that unique_ptr is simpler and more idiomatic. |
Issue description
Once again I've been playing with my toys - valgrind, pybind11@ #2741 and CPython 3.9.1
Good news: We're almost there. Almost a clean run.
Bad news: Some casters leak like a broken faucet.
More specifically,
py::cast(new int{})
leaks and so do all the other casters that copy the data -list_caster
fromstl.h
is another example. Another thing that leaks isnew CustomClass{}
withpy::return_value_policy::move
.While
py::cast(new int{});
could be argued that it's a user error, that argument doesn't take into account the following:Even our docs have
return new Example();
and "don't mix things that don't play well together" in this case is very subtle of a contract to introduce after so many years of people assuming that it just works (tm).Reproducible example code
Calling all three functions results in these valgrind errors:
In case people find it easier to parse the embedded interpreter version:
According to @YannickJadoul's analysis, this happens because the affected casters don't end up owning the returned pointer. The casters copy the data and end up owning the copied data, but leak the returned pointer.
The text was updated successfully, but these errors were encountered: