pybind / pybind/pybind11

Cannot get std::vector from binded member function having passed by reference std::vector as parameter

Open
#1,230 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have a dll Core, class __declspec(dllexport)'ing a certain class X. This class is in charge of some progressive calculation, successive values being kept track of in an "array". (I don't know yet which c++/python structure is the best for what I am trying to achieve.) This "array" can either be 1) a member of X or 2) returned by a member function of X. I am using this dll in another pyd dll, Binder, which as its name indicates, is in charge of doing python bindings of X (c++) member functions. Then I import the Binder.pyd dll in a python file, and I try to use python functions.

At the beginning, the "array" wasn't a member of X, which had a function void F(std::vector<double> & array) in charge of updating the array, but any run of the python file using the binded python version of F was triggering an access violation, precisely whenever resize or push_back where called on the std::vector<double>. I binded btw std::vector<double> as follows :

py::class_<std::vector<double>>(m, "stdvectordouble")
	.def(py::init<>())
	.def("clear", &std::vector<double>::clear)
	.def("pop_back", &std::vector<double>::pop_back)
	.def("push_back", (void (std::vector<double>::*)(const double &)) &std::vector<double>::push_back)
	.def("__len__", [](const std::vector<double> &v) { return v.size(); })
	.def("__iter__", [](std::vector<double> &v) {
            return py::make_iterator(v.begin(), v.end());
}, py::keep_alive<0, 1>());

I tried std::share_ptr<double> + int (size) instead, same type of issue as well. I suspect that my problem is ultimately caused by this :

https://support.microsoft.com/en-us/help/172396/you-may-experience-an-access-violation-when-you-access-an-stl-object-t

though I don't really know how to put the proposed solution in practice in my case.

So that I have at the switch to keeping the "array" as a member of X, but I really don't know what the better structure to give to it to by able to easily bind its get function so that I coudl extract the "array" in my python code. (I would also like to have a c++ code recyclable enough in other c++ contexts, that is : I don't want to have the signature of F to be too much pybind11-dependent.)

Remark : at the origin, Core and Binder dll's were one single dll, and I wasn't having the acces violation error triggered at run time.

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

No repository files or tests are named. Start by reproducing the Python call across the separate Core and Binder DLLs, then inspect the std::vector reference binding and DLL boundary described in the issue. Done means the access violation is resolved and the progressive array can be retrieved from Python without making the C++ API unnecessarily pybind11-dependent.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, developer-experience
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.