Деструктор PyBind11 не вызывается? - PullRequest
1 голос
/ 01 апреля 2019

У меня есть c++ класс, завернутый в PyBind11.Проблема заключается в следующем: когда скрипт Python заканчивается, c++ destructor не вызывается автоматически.Это вызывает неопрятный выход, потому что деструктор должен освобождать сетевые ресурсы.

В качестве обходного пути необходимо явно удалить объект Python, но я не понимаю, почему!

Может кто-нибудь объяснить, что здесь не так и как автоматически вызывать destructor, когда объект Python собирается сборщиком мусора?

Код привязки 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:

#Function to callback
def print_string(str):
    print("Python; " + str)

lstnr = listener(print_string, 'tcp://127.0.0.1:9001', clientCertPath, serverCertPath, proxyCertPath, desiredSources, 'time_series_data', enableCurve, enableVerbose)

#Run for a minute
cnt = 0
while cnt < 60:
    cnt += 1
    time.sleep(1)

#Need to call the destructor explicity for some reason    
del lstnr
...