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

Offen
#5,181 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 4 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
cpp, python

Rechercherichtung

Beginne mit dem reproduzierbaren pybind11-Modul und vergleiche den set_data-Parameter als const Eigen::VectorXd& gegenüber Eigen::Ref unter den aufgeführten Windows-, Visual-Studio-, Python- und pybind11-Versionen. Verfolge das Konvertierungsverhalten durch den enthaltenen pybind11/eigen.h-Pfad und vergleiche es mit dem stable branch. Abgeschlossen ist die Arbeit, wenn die Ursache der Zugriffsverletzung identifiziert und ein verifizierter Fix für alle gemeldeten Umgebungen definiert oder angewendet wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

triage
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
Vorherrschende Sprache
C++
Sterne
18k
Forks
2.3k
Ø Merge
5 T. 7 Std.
Gemergte PRs (30 T.)
53

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pybind/pybind11

Alle Issues in pybind/pybind11

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.