mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Use weakref to clean up captured function object in def_buffer (#2634)
This commit is contained in:
parent
06a54018c8
commit
7d6713a46d
@ -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:
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user