pybind / pybind/pybind11

[BUG]: Function with Param of type `const Eigen::VectorXd&` causes "windows fatal exception: access violation" on Windows

Open
#5,181 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

v2.10.4

Problem description

When calling function with parameter const Eigen::VectorXd&, it fails with "windows fatal exception: access violation" on Windows, compiled with VS2022, Python 3.12, and Pybind v2.10.4.

However, the example works well with Python 3.10 + VS2019 + Pybind v2.10.4.
Besides, it also works well in Python 3.12 + VS2022 + Pybind current stable branch.

I know from the Documentation that it recommend using Eigen::Ref for reference. Using Eigen::Ref, Python 3.12 + VS2022 + Pybind v2.10.4 also works.

That said, using either Pybind current stable branch or Eigen::Ref can make it work.

I have been struggling on this issue for a while.

I want to understand how things go wrong:

  • Why Eigen::Ref works, but const Eigen::VectorXd& not?
  • Why Pybind current stable branch works?
Reproducible example code
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/functional.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <vector>
#include <Eigen/Eigen>

namespace py = pybind11;


class ArrayDynamic {
 using Vector =Eigen::VectorXd;

 private:
  Vector data_;

 public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW

  ArrayDynamic() = default;
  virtual ~ArrayDynamic() = default;

  explicit ArrayDynamic(size_t dim) { data_.resize(dim); }
  auto data() const -> const Vector& { return data_; }
  auto data() -> Vector& { return data_; }
};


PYBIND11_MODULE(promote, m)
{
    auto math = m.def_submodule("math");
    py::class_<ArrayDynamic>(math, "ArrayDynamic")
        .def(py::init<size_t>(), "Create an array of given size.", py::arg("dim"))
        .def("data", py::overload_cast<>(&ArrayDynamic::data), py::return_value_policy::reference_internal, "internal")
        .def( "set_data",
            [](ArrayDynamic& array,
               const Eigen::VectorXd& data)
            {
                array.data() = data;
            },
            "Set data.");
}

For a full example, see https://github.com/huweiATgithub/pybind_fail_example



### Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible pybind11 module and compare the set_data parameter as const Eigen::VectorXd& versus Eigen::Ref under the listed Windows, Visual Studio, Python, and pybind11 versions. Trace the conversion behavior through the included pybind11/eigen.h path and compare it with the stable branch. Done means identifying the cause of the access violation and defining or applying a verified fix across the reported environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.