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 ()
Есть идеи о том, что нужно сделать, чтобы правильно запустить эту программу?