-
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
Embedded python: Pass an C++ object by reference #1536
Comments
is this different that #1508? Code doesn't compile, need a more complete example. |
The code is similar but this is a different discussion. All I wanted was how to manage reference count for the scenario I mentioned above. |
If you pass around If you use the python c api, like raw |
I have a problem where the code is panicing when I am making python call from C++. Its on the line of the above discussion: class TplatPyModule:
Here is binding code Here epInfo_(of type tplatEndpointInfo*) is defined in the base class smaTplatInterface, class attribute((visibility("hidden"))) tplatPyWrapper :
}; py::module tplatPyWrapper::module_; PYBIND11_MAKE_OPAQUE(tplatEndpointInfo); PYBIND11_EMBEDDED_MODULE(TplatPythonInterface, m) { The code is panicing at the time when we are trying to call the connect function as defined under. static void tplatPyWrapper::initialize() { tplatPyWrapper::tplatPyWrapper() { SmaStatus (gdb) bt |
Hi Guys, |
I have a use-case where I have implemented an API in python and that needs to be invoked by C++.
struct ABC {
ABC(int _i, int _j) {
i = _i;
j = _j;
}
int i;
int j;
};
struct listABC {
listABC() {}
std::string name_;
std::vector lst_;
};
PYBIND11_MAKE_OPAQUE(std::vector);
PYBIND11_MODULE(cc11binds, m) {
py::class(m, "ABC")
.def(py::init<double, double>())
;
py::class_(m, "listABC")
.def_readwrite("name", &listABC::name_)
.def_readwrite("lst", &listABC::lst_)
;
py::bind_vector<std::vector>(m, "ABCTest", py::module_local(false));
}
struct listABC { std::string name_; std::vector lst_; };
cumabc(&abcList) -> this takes reference to vector object. This works fine:
before cumabc, in C++ --- 2
[i:1 :: j:2]
after cumabc, in C++ --- 2
[i:1 :: j:2, i:3 :: j:4]
Where (3,4) is added by python interpreter.
cumlistABC(&listAbcWrapper) -> This takes reference to listABC object.
My code encounters both the type of pass by reference. Both listAbcWrapper and abcList are passed by class A to B. I was curious if we need to manage the reference count of listAbcWrapper and abcList (i.e. increase the reference count till their lifetime in A. A can later call a freelistAbc and freeCumAbc).
My architecture is:
|-----------------| |------------------| |--------------------|
| Main Driver A | --> | C++ Wrapper B | -----> | Python Callee C |
|-----------------| |------------------| |--------------------|
The text was updated successfully, but these errors were encountered: