Skip to content
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

Crash when assigning one matrix to another #93

Closed
kjohnsen opened this issue Sep 22, 2021 · 2 comments
Closed

Crash when assigning one matrix to another #93

kjohnsen opened this issue Sep 22, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kjohnsen
Copy link

Believe it or not, I actually ran into another, separate heap corruption error when copy-assigning one matrix to another, e.g.,

mre.cpp:

#include "mre.h"

namespace py = pybind11;

class MatrixHolder {
public:
  MatrixHolder(size_t d) {
    A = arma::Mat<double>(d, d, arma::fill::eye);
    std::cerr << "filled arma matrix\n";
  };
  arma::Mat<double> A;
};

PYBIND11_MODULE(pymod, m) {
  py::class_<MatrixHolder>(m, "MH").def(py::init<size_t>())
      .def_readwrite("A", &MatrixHolder::A);
};

pymod_test.py:

print('testing matrix copy assignment')
d = 30
mh1 = MH(d)
mh2 = MH(d)
mh1.A = mh2.A  # <- fails here
print('successfully copied mh2.A to mh1.A')

The matrices have to be large enough to use dynamically allocated memory.
And this bug has shown up for me on Windows with Clang 11/12 and MSVC, as well as WSL Ubuntu using GCC.

RUrlus added a commit that referenced this issue Sep 22, 2021
@RUrlus RUrlus self-assigned this Sep 22, 2021
@RUrlus RUrlus added the bug Something isn't working label Sep 22, 2021
RUrlus added a commit that referenced this issue Sep 22, 2021
@RUrlus
Copy link
Owner

RUrlus commented Sep 22, 2021

@kjohnsen thanks for reporting. I could reproduce and it's a bug.
Fixed in 8adfd96.

However, you are not copying when doing this! What is happening is that the mh1 borrows the memory of the matrix of mh2. Remember, we avoid copying as much as possible so the type-cast, which you implicitly use, borrows by default.

What you are doing with mh1.A = mh2.A:

  1. convert the arma::mat A from mh2 to a Numpy array
  2. pass that Numpy array to the setter for mh1
  3. convert the Numpy array, by borrowing, to the arma::mat B in the setter

Now both mh1.A and mh2.A are aliasing the same memory!

For the bug, as the Numpy array will not own the data it would, wrongly, trigger the swap-copy conversion which should not allow not-owndata arrays.

I'll create a patch release later this week but for now you can use the unstable branch.

@RUrlus RUrlus closed this as completed Sep 22, 2021
@kjohnsen
Copy link
Author

All right, thanks! I forgot borrowing was the default, which in hindsight was clear from the memory problems

RUrlus added a commit that referenced this issue Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants