sim_wrapper.h
#pragma once
#include <pybind11/embed.h>
namespace n_sim
{
namespace py = pybind11;
// __attribute__ needed for pybind to work
class __attribute__((visibility("default"))) Sim
{
public:
Sim();
// python sim class instance we're using
py::object sim;
private:
// python module want to load into our C++ code.
py::module SimWrapModule;
// Needed to initialize pybind. As soon as this goes out of scope, pybind is un-initialized.
py::scoped_interpreter guard{};
};
}
sim_wrapper.cpp
#include <iostream>
#include "sim_wrapper.h"
using namespace n_sim;
Sim::Sim()
{
// import python module
SimWrapModule = py::module::import("wrapper");
// catch return values from python functions
sim = SimWrapModule.attr("initialize_sim")();
}
int main()
{
Sim* SimWrap = new Sim();
py::str name = SimWrap->sim.attr("name");
std::cout << name.cast<std::string>() << std::endl;
return 0;
}
运行该程序时,它将打印name
并关闭程序,但留下三个未清理的进程。在这些过程中尝试gstack
,并且所有过程的输出类似,
$ gstack 22763
#0 0x00000036f42df3c8 in poll () from /lib64/libc.so.6
#1 0x00007f012a43867b in poll_poll () from /opt/debesys/ext/linux/x86-64/release/lib/python2.7/lib-dynload/select.so
#2 0x00007f01346b50cb in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#3 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#4 0x00007f0134631190 in function_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#5 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#6 0x00007f01346b1956 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#7 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#8 0x00007f01346b5ab5 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#9 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#10 0x00007f01346b5ab5 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#11 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#12 0x00007f0134631265 in function_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#13 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#14 0x00007f01346b1956 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#15 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#16 0x00007f01346b5ab5 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#17 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#18 0x00007f0134631265 in function_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#19 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#20 0x00007f01346b1956 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#21 0x00007f01346b5b63 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#22 0x00007f01346b5b63 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#23 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#24 0x00007f0134631190 in function_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#25 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#26 0x00007f013460e21d in instancemethod_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#27 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#28 0x00007f013466ac6f in slot_tp_init () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#29 0x00007f01346697ef in type_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#30 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#31 0x00007f01346b2281 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#32 0x00007f01346b5b63 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#33 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#34 0x00007f01346b5ab5 in PyEval_EvalFrameEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#35 0x00007f01346b65cd in PyEval_EvalCodeEx () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#36 0x00007f0134631190 in function_call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#37 0x00007f01345ff793 in PyObject_Call () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#38 0x00007f01346aff67 in PyEval_CallObjectWithKeywords () from /opt/debesys/ext/linux/x86-64/release/lib/libpython2.7.so.1.0
#39 0x0000000000417299 in pybind11::detail::simple_collector<(pybind11::return_value_policy)1>::call(_object*) const ()
#40 0x0000000000413c48 in _ZNK8pybind116detail10object_apiINS0_8accessorINS0_17accessor_policies8str_attrEEEEclILNS_19return_value_policyE1EJEEENS_6objectEDpOT0_ ()
#41 0x00000000004054b7 in n_sim::Sim::Sim() ()
#42 0x00000000004055f5 in main ()
关于干净运行此程序需要做什么的任何想法?
答案 0 :(得分:1)
之所以发生这种情况,是因为Python模块所拥有的某些资源没有被释放。在python模块中编写了python函数stop
,最后在C ++中调用了该函数,从而解决了此问题。
就在返回之前,我现在打电话给SimWrap->sim.attr("stop")();