-
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
Dynamic attributes of objects stored as std::shared_ptr's in std::vector's #1941
Comments
I can't repro this with gcc or clang. I'm suspecting there was a typo or something when you have tried this. Feel free to reopen, if you still have a problem. |
Hey @bstaletic , thanks for taking the time to investigate my report. I still experience this issue on a just-updated Ubuntu 20.04 box. This is the software I am using:
If you need any other information please let me know. |
The problem is that I can't reproduce your issue at all, from what you've posted originally. Note that I am using latest master. Even version 2.5.0 is fairly odd. Can you clone the repo and try with "the latest and greatest"? |
The same thing happens with the just-cloned version. |
I don't even have |
I'm sorry but what do you mean? The point is that in my case the following script works only if the
how are you testing the code otherwise? |
Ah... I skipped |
@lorenzo-rovigatti Here's whats going wrong: So in the case where you have a variable referencing the Python object, this object is kept alive/not garbage-collected, and the following accesses of In the other case, a Python object wrapping the C++ object is created, an attribute How to solve this? This is harder, indeed. The suggestion of the SO answer you reference would work: if you can store it at the C++ side, it'll be persistent. |
Pffff, that's not working :-( Thing is: the I'm really not sure who's responsibility it is on the C++ side to keep those Python objects referenced. I mean ... you could leak them (increase the refcount by 1 and never decrease), but that's not a solution in general, either, ofc. |
Thanks @YannickJadoul , I appreciate your attempts and I agree it's probably a very hard issue to solve (and would require some ad-hoc method on c++ side, probably). I guess I'll have to live with this inconsistency :) |
@EricCousineau-TRI, @rwgk, @rhaschke, this already slipped my mind again, but here's another issue related to #2646 (comment) |
Ah, I see - the object has some Python-specific memory that gets lost when This may influence the solutions that we choose for #1333 - since However, that will break with less flexible holders, e.g. A reference cycle via |
@EricCousineau-TRI pointed me to this issue. Context: redesign related to #2646. My opinion(s): simply document as unsupported. Dynamic attributes are kind-of dirty to start out with (although they are a must have as an option for dealing with the dirty real world). Adding complexity to pybind11 for use cases like the |
I would like to add dynamic attributes to objects stored as shared pointers in std::vector's. I have read the docs and I'm still not sure whether this is truly possible or no. However, while performing some tests I have discovered an inconsistency that pybind11 devs might be interested in.
This inconsistent behaviour can be reproduced with the following c++ code:
which can be compiled (at least on my Linux box) with
Now, on the python side the following snippet works or not depending whether the line starting with
myfoo
is commented or not:Is there some hidden caching going on here (as suggested by this answer)? Is it possible to add dynamic attributes to the instances stored in the
foos
vector without keeping an explicit reference to each of them?The text was updated successfully, but these errors were encountered: