embedded python - quit()でSegmentation Fault
% ./build/hoge >>> import hoge >>> quit() zsh: segmentation fault ./build/hoge % gdb ./build/hoge ... (gdb) run ... >>> import hoge >>> quit() Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7f02b590b950 (LWP 7323)] 0x00007f02b91aac44 in ?? () from /usr/lib/libpython2.6.so.1.0 (gdb) where #0 0x00007f02b91aac44 in ?? () from /usr/lib/libpython2.6.so.1.0 #1 0x00007f02b918bf47 in ?? () from /usr/lib/libpython2.6.so.1.0 #2 0x00007f02b918e960 in PyDict_SetItem () from /usr/lib/libpython2.6.so.1.0 #3 0x00007f02b9190c1f in _PyModule_Clear () from /usr/lib/libpython2.6.so.1.0 #4 0x00007f02b9207bb3 in PyImport_Cleanup () from /usr/lib/libpython2.6.so.1.0 #5 0x00007f02b9213e7d in Py_Finalize () from /usr/lib/libpython2.6.so.1.0 #6 0x00007f02b9213fb8 in Py_Exit () from /usr/lib/libpython2.6.so.1.0 #7 0x00007f02b92140f2 in ?? () from /usr/lib/libpython2.6.so.1.0 #8 0x00007f02b921432d in PyErr_PrintEx () from /usr/lib/libpython2.6.so.1.0 #9 0x00007f02b92154ed in PyRun_InteractiveOneFlags () from /usr/lib/libpython2.6.so.1.0 #10 0x00007f02b92155fe in PyRun_InteractiveLoopFlags () from /usr/lib/libpython2.6.so.1.0 #11 0x00000000004e01b7 in PythonShell::operator() (this=0x7fffc2680c7f) at console.cpp:141 #12 0x00000000004e110f in boost::detail::thread_data<boost::reference_wrapper<PythonShell> const>::run (this=0x24ea2b0) at /usr/local/include/boost-1_40/boost/thread/detail/thread.hpp:100 #13 0x00007f02b7ed2410 in thread_proxy () from /usr/local/lib/libboost_thread-gcc45-mt-1_40.so.1.40.0 #14 0x00007f02b9cec3ba in start_thread () from /lib/libpthread.so.0 #15 0x00007f02b779ffcd in clone () from /lib/libc.so.6 #16 0x0000000000000000 in ?? () (gdb)
ConsoleをThreadで回してるからかな、うんそうだきっと。
boost-1.39で最小のサンプル
#include <iostream> #include <Python.h> #include <boost/python.hpp> using namespace boost::python; class Hoge { public: void hoge() { std::cout<<"hoge--"<<std::endl; } }; class HogeHoge : public Hoge { }; BOOST_PYTHON_MODULE(hoge) { class_<Hoge>("Hoge") .def("hoge", &Hoge::hoge) ; class_<HogeHoge, bases<Hoge > >("HogeHoge") ; } int main() { using namespace boost::python; if(PyImport_AppendInittab("hoge", inithoge) == -1) throw "hogeeee"; Py_Initialize(); PyRun_InteractiveLoop(stdin, "hoge"); Py_Finalize(); return 0; }
% gdb ./a.out ... (gdb) run Starting program: /.../a.out [Thread debugging using libthread_db enabled] >>> import hoge >>> quit() [New Thread 0x7f4865819710 (LWP 7901)] Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7f4865819710 (LWP 7901)] 0x00007f4865216c44 in type_dealloc (type=0x7f48655ba940) at ../Objects/typeobject.c:2609 2609 ../Objects/typeobject.c: No such file or directory. in ../Objects/typeobject.c (gdb) where #0 0x00007f4865216c44 in type_dealloc (type=0x7f48655ba940) at ../Objects/typeobject.c:2609 #1 0x00007f48651f7f47 in insertdict (mp=0x1df28b0, key=0x7f48657aacf0, hash=-1492816253029089373, value=0x60d700) at ../Objects/dictobject.c:459 #2 0x00007f48651fa960 in PyDict_SetItem (op=0x1df28b0, key=0x7f48657aacf0, value=0x60d700) at ../Objects/dictobject.c:701 #3 0x00007f48651fcc1f in _PyModule_Clear (m=<value optimized out>) at ../Objects/moduleobject.c:138 #4 0x00007f4865273bb3 in PyImport_Cleanup () at ../Python/import.c:497 #5 0x00007f486527fe7d in Py_Finalize () at ../Python/pythonrun.c:434 #6 0x00007f486527ffb8 in Py_Exit (sts=1700505920) at ../Python/pythonrun.c:1714 #7 0x00007f48652800f2 in handle_system_exit () at ../Python/pythonrun.c:1116 #8 0x00007f486528032d in PyErr_PrintEx (set_sys_last_vars=1) at ../Python/pythonrun.c:1126 #9 0x00007f48652814ed in PyRun_InteractiveOneFlags (fp=0x7f48647436a0, filename=0x407f70 "hoge", flags=0x7fff6d835f50) at ../Python/pythonrun.c:843 #10 0x00007f48652815fe in PyRun_InteractiveLoopFlags (fp=0x7f48647436a0, filename=0x407f70 "hoge", flags=0x7fff6d835f50) at ../Python/pythonrun.c:760 #11 0x0000000000402782 in main () (gdb)
・・・。
python2.6_dに御光臨願う前に思い出した、Py_FinalizeとBoost::pythonの絡みの件
Getting started
Being able to build is nice, but there is nothing to build yet. Embedding the Python interpreter into one of your C++ programs requires these 4 steps:
#include
Call Py_Initialize() to start the interpreter and create the _main_ module.
Call other Python C API routines to use the interpreter. NoteNote that at this time you must not call Py_Finalize() to stop the interpreter. This may be fixed in a future version of boost.python.
(Of course, there can be other C++ code between all of these steps.)
Now that we can embed the interpreter in our programs, lets see how to put it to use...
ま、まじでーー
% grep -r Py_Finalize ~/Sources/boost_1_39_0/libs/python/test/*.cpp /home/*/Sources/boost_1_39_0/libs/python/test/exec.cpp: // Boost.Python doesn't support Py_Finalize yet. /home/*/Sources/boost_1_39_0/libs/python/test/exec.cpp: // Py_Finalize(); /home/*/Sources/boost_1_39_0/libs/python/test/import_.cpp: // Boost.Python doesn't support Py_Finalize yet. /home/*/Sources/boost_1_39_0/libs/python/test/import_.cpp: // Py_Finalize();
ま、まじだーー!!
あれから5日後・・・
結論:Py_Finalizeはまったく関係ない気配。ナンテコッタ
SIGSEGVをなくすには、継承グラフのルートにclass_
とにかくno_initが含まれる定義から継承させれば回避できる ・・・今のところは。
原因は相変わらず不明