您如何调用析构函数,或者何时在pybind11中从python自动调用析构函数?

tacos_tacos_tacos

如果我在中使用了take_ownership返回值策略pybind11,并且调用了一个返回a的函数std::vector<Dog>,那么如何确保调用向量及其内容的析构函数?它与超出python客户端代码范围无关吗?

当Python垃圾收集对象(并调用CPython API钩子)时,事物将被破坏。

请参阅此处获取Python文档:

https://docs.python.org/3.7/reference/datamodel.html#objects-values-and-types https://docs.python.org/3.7/reference/datamodel.html#object。德尔

从不显式销毁对象。但是,当它们变得不可访问时,它们可能会被垃圾回收。[...]

特定于pybind,这是删除时调用的分配位:

  1. https://github.com/pybind/pybind11/blob/25abf7e/include/pybind11/detail/class.h#L389
  2. https://github.com/pybind/pybind11/blob/25abf7e/include/pybind11/detail/class.h#L346
  3. https://github.com/pybind/pybind11/blob/25abf7e/include/pybind11/detail/class.h#L327
  4. https://github.com/pybind/pybind11/blob/25abf7e/include/pybind11/pybind11.h#L1339

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章