Skip to content

Fix ElementArrayView lifetime issues with Python - #3895

Merged
jl-wynen merged 2 commits into
mainfrom
no-tmp-ref-return
May 4, 2026
Merged

jl-wynen merged 2 commits into
mainfrom
no-tmp-ref-return

Conversation

@jl-wynen

Copy link
Copy Markdown
Member

Fixes #3894

@SimonHeybrock SimonHeybrock self-assigned this Apr 29, 2026
.def("__iter__", [](const ElementArrayView<T> &self) {
return py::make_iterator(self.begin(), self.end());
});
py::keep_alive<0, 1>());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is confusing, but am I right that adding keep_alive here is the only bit that has changed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, __getitem__ used to have py::return_value_policy::reference) but I removed that. We don't need it because it returns a Python object anyway and copying/moving that handles reference counts automatically.

jl-wynen added 2 commits May 4, 2026 10:54
ElementArrayView is only used for Python objects. Those are not copied here anyway. So there is no need for `return_value_policy::reference`, just let Python ref counting do its job.
@jl-wynen
jl-wynen enabled auto-merge May 4, 2026 08:54
@jl-wynen
jl-wynen force-pushed the no-tmp-ref-return branch from 71ff472 to 8092c8a Compare May 4, 2026 08:54
@jl-wynen
jl-wynen merged commit df65430 into main May 4, 2026
4 checks passed
@jl-wynen
jl-wynen deleted the no-tmp-ref-return branch May 4, 2026 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ElementArrayView lifetime errors in Python

3 participants