你如何调用析构函数或什么时候从 pybind11 中的 python 自动调用它?

How do you call the destructor or when is it automatically called from python in pybind11?

如果我在 pybind11 中使用 take_ownership return 值策略,并且我调用了 return 的函数,例如 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.del

Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. [...]

特定于 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