-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Object lifetime management #609
Comments
This is usually what Return Value Policies are for, and if you want to return an unowned pointer from a function you would specify py::class_<Bell>(m, "Bell"); // no init
m.def("make_bell", []() { return new Bell; }, return_value_policy::reference); Note that py::class_<Bell>(m, "BellClass"); // no init
m.def("Bell", []() { return new Bell; }, return_value_policy::reference); Alternatively, if you really need to keep the constructor, you could use |
Closing: There's been no reply for a month, so I assume this is resolved. |
I am trying to solve an ownership problem in pybind11.
I have to configure a hierarchy of objects in python.
In c++ this is done like:
The python equivalent:
In c++ the destructor of the Inputvariable delete's its Bell's.
Thus the InputVariable owns its Bells(and is responsible for cleaning them up).
When using python, this results in a conflict, because when creating the Bell's these are owned by the python interpreter, adding those with the addTerm function does not transfer this membership:
During destruction (or memory collection in python) the d'tor of InputVariable is called which contains these statements:
The result is: kabang, since the Bell's are destructed twice.
What is the best approach to solve this, without changing the existing c++ code?
I hope this is the right platform to ask this question. If not could you please refer me to the place that is?
The text was updated successfully, but these errors were encountered: