-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Could unchecked views prevent garbage collection of the underlying arrays? #961
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
Comments
The unchecked reference class itself is meant to be extremely light: it is really nothing more than a pointer and shape/stride calculator. Adding a reference to the underlying array would make it non-trivially-destructible, which while not a huge deal, is no longer quite as light-weight. The example is indeed a problem; I'm tempted to qualify the |
I would prefer referencing but erroring on lost references would certainly be already an improvement. |
This should mitigate accidental invocation on a temporary array. Fixes pybind#961.
Upon further reflection, I am not certain this is the right approach: the function returning an array_t<> could well be returning a reference, say, to an attribute that is held somewhere on the Python side, in which case there is no issue with the above idiom. I guess if you want to make this safe without actually holding a reference, you should check the refcount on the array when creating the view? |
Adding a reference for the duration of the proxy object is another option. I'm not dead set against it, but I don't really see a huge advantage here, either. (Also see #599, #617, and #746 for the (rather lengthy) discussion that led up to this; there were multiple proposals for unchecked access considered before we finally settled on the current approach.) |
This should mitigate accidental invocation on a temporary array. Fixes #961.
Issue description
Unchecked views appear to not prevent garbage collection of the underlying array (object). Unless I am mistaken, it should be possible for them to incref the underlying object upon construction of the unchecked view and decref it upon destruction, for a minimal cost; I do not see a case where the opposite behavior is desirable.
Reproducible example code
The text was updated successfully, but these errors were encountered: