Using the Python interpreter
Hello All! I've the problem with the next code: class MyWindow { ... MyWindow() {} ~MyWindow() {} MyOperation() { Py_Initialize(); handle<> main_module(borrowed(PyImport_AddModule("__main__"))); handle<> namespace(borrowed(PyModule_GetDict(main_module.get()))); try { handle<>(PyRun_String("hello = file('c:\\hello.txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, namespace.get(), namespace.get())); } catch(error_already_set) { ... } Py_Finalize(); } }; When I start this code, that's all right. If I move Py_Finalize() to destructor, the program work, but when I close the program an exeption rise (Py_FatalError("UNREF invalid object")). Next, I exclude Boost classes: ... MyOperation() { PyObject* dict = PyModule_GetDict(PyImport_AddModule("__main__")); PyRun_String("hello = file('c:\\hello.txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, dict, dict); } So the program work well and there is no exeptions at exit. What is my problem? How can I use Boost in right way? Please Help Thanks Dima
dima_mouromtsev wrote:
Hello All!
Hi.
I've the problem with the next code:
[snip]
When I start this code, that's all right.
If I move Py_Finalize() to destructor, the program work, but when I close the program an exeption rise (Py_FatalError("UNREF invalid object")).
Next, I exclude Boost classes:
[snip]
So the program work well and there is no exeptions at exit.
What is my problem? How can I use Boost in right way?
I haven't been able to reproduce your problem with Boost 1.30.0, but I have run into a similar problem in the past. The cause is that Boost.Python contains a few global and static Python objects, which get destructed after the program ends. Since you call Py_Finalize() before the end of the program, Python has already shutdown and released these objects, which causes the problem. This is of course a bug in Boost.Python. Dave and I are trying to fix it soon, but for now, you should just not call Py_Finalize. This means your program might leak some memory, but this should be reclaimed by your operating system anyway. Hope that helps, Dirk Gerrits
Oh and I forgot to mention, most Boost.Python communication goes through Python's C++ sig, so you might get (even) quicker responses there: http://www.python.org/sigs/c++-sig/ Regards, Dirk Gerrits
participants (2)
-
dima_mouromtsev
-
Dirk Gerrits