-
Notifications
You must be signed in to change notification settings - Fork 202
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]: Storing const reference to Eigen types in a class when working with nb::Dref
causes data corruption.
#802
Comments
I'm surprised that you can just cast a |
@wjakob thanks for your reply. The get started documentation states the following:
I assumed that in case of non-contiguous data, a copy operation was performed in order to enable the many Eigen optimisations that can only be performed on contiguous memory. However, I now see that "slices" are mentioned along side row-major and column-major layouts. Nevertheless, it took me a surprising amount of time to debug this issue since I was testing with |
void correct(const nb::DRef<Eigen::ArrayXi> & b) {
const Eigen::ArrayXi & ref = b;
} Compiles fine for 2 reasons:
Because the reference is local, it extends the lifetime of the temporary used to initialize it. void corrupt(const nb::DRef<Eigen::ArrayXi> & b) {
const ArrayRef ref(b);
ref.size();
} Also compiles fine, but accesses invalid memory, because ArrayRef's constructor extends the lifetime of the temporary only until it returns. And its data member does not, because it is initialized from another reference. This is plain C++, nothing special to Eigen or even nanobind. |
Thank you @WKarel, I will close this issue. |
Problem description
Storing a const reference to an Eigen object as part of a class/structure that is initialised with a
nb::Dref
object compiles without any issues but results in data corruption.If this is intended behaviour, then a compile-time error should be thrown.
Reproducible example code
C++ file
Python file
Result
The text was updated successfully, but these errors were encountered: