PyBind11析构函数未调用?

2024-06-28 19:52:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用PyBind11包装的c++类。问题是:当Python脚本结束时,c++destructor没有被自动调用。这会导致不整洁的退出,因为网络资源需要由析构函数释放。在

作为一种解决方法,有必要显式删除Python对象,但我不明白为什么!在

有人能解释一下这里出了什么问题,以及在对Python对象进行垃圾回收时如何自动调用destructor?在

Pybind11绑定代码:

py::class_<pcs::Listener>(m, "listener")
    .def(py::init<const py::object &, const std::string &, const std::string &, const std::string &, const std::string &, const std::set<std::string> &, const std::string & , const bool & , const bool & >(), R"pbdoc(
    Monitors network traffic.

    When a desired data source is detected a client instance is connected to consume the data stream.

    Reconstructs data on receipt, like a jigsaw.  Makes requests to fill any gaps.  Verifies the data as sequential.

    Data is output by callback to Python.  Using the method specified in the constructor, which must accept a string argument.
)pbdoc");

在Python中:

^{pr2}$

Tags: theto对象py脚本datastringis