Commit 7d6713a4 by Yannick Jadoul Committed by GitHub

Use weakref to clean up captured function object in def_buffer (#2634)

parent 06a54018
...@@ -1315,7 +1315,8 @@ public: ...@@ -1315,7 +1315,8 @@ public:
return *this; return *this;
} }
template <typename Func> class_& def_buffer(Func &&func) { template <typename Func>
class_& def_buffer(Func &&func) {
struct capture { Func func; }; struct capture { Func func; };
auto *ptr = new capture { std::forward<Func>(func) }; auto *ptr = new capture { std::forward<Func>(func) };
install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
...@@ -1324,6 +1325,10 @@ public: ...@@ -1324,6 +1325,10 @@ public:
return nullptr; return nullptr;
return new buffer_info(((capture *) ptr)->func(caster)); return new buffer_info(((capture *) ptr)->func(caster));
}, ptr); }, ptr);
weakref(m_ptr, cpp_function([ptr](handle wr) {
delete ptr;
wr.dec_ref();
})).release();
return *this; return *this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment