-
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
Pybind incorrectly deregisters instances (by type comparison), yields errors when C++ recycles memory #1238
Comments
I'm not entirely sure I see what the issue is. Is it that there are two entries in As for the proposed fix, there is a strong reason for the type comparison, namely that two separate objects can share the same address, typically when one object is the first member of a class (or first member of a first member, etc.). In other words, something like this can trigger it: struct A { /* ... */ };
struct B {
A a;
/* ... */
};
py::class_<A>(m, "A");
py::class_<B>(m, "B")
.def("a", [](B &self) { return &self.a; }, py::return_value_policy::reference_internal); in which case both the |
To preface, this is for my
Ah, that makes sense! Thank you! I guess a slightly more costly solution then is to compare instance and type.
Yup!
That is true, but that would require the user to intervene, |
I ran into an interesting (read: Heisen) bug that occurred when I had code that looked like this:
After C++ destroyed the first instance, the second instance was allocated into the same address, when casting the instance back,
pybind
registered a new instance (inget_internals().registered_instances
) when the same ptr-value, but a different instance.In
deregister_instance_impl
, it finds all values that match the given ptr, and only checks if thePy_TYPE
is the same.The fix in this case is just to compare instances (if there wasn't a strong reason for getting the type). An example change:
https://github.com/EricCousineau-TRI/pybind11/blob/feature/unique_ptr_arg_pr1-wip/include/pybind11/detail/class.h#L225
Even though I encountered this for my
unique_ptr
PRs, I believe it could still be an issue when returning bare pointers, say:The text was updated successfully, but these errors were encountered: