You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been trying to wrap this using code like this:
m.def("set_callback",
[](Owner* owner, const std::function<void(int)>& cb)
{
set_callback(
owner,
[](int i, void* data) // Expect std::function as client data.
{
auto fun = static_cast<std::function<void(int)> >(data);
(*fun)(i);
},
&cb); // Pass std::function as client data.
},
py::keep_alive<1, 2>()); // owner object should keep callback function alive.
But this doesn't work. I get a bad function exception when the cb is called from the C code. As a work around I've been cloning the std::function before passing it as client data. This works but leaks. Is this a bug or is there another way I should be able to make this work?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm wrapping a C API with a number of calls taking C style callback functions. ie this kind of pattern:
I've been trying to wrap this using code like this:
But this doesn't work. I get a bad function exception when the cb is called from the C code. As a work around I've been cloning the std::function before passing it as client data. This works but leaks. Is this a bug or is there another way I should be able to make this work?
Beta Was this translation helpful? Give feedback.
All reactions