jookos
jookos

Reputation: 75

pygtk and native gtk_main(); mixing

I'm trying to develop a C-based application which embeds one or more Python interpreters. I'm using gtk-things in the C-parts, and logically calling gtk_main() within that.

The Python interpreters are created in separate pthreads using Py_NewInterpreter(), and basically running forever (a 'while True' loop is added to the end in case the script terminates).

Now I'd like to use pyGTK in those scripts, which seems to work until there's a callback (signal emitted). You can register signal handlers to point to parts in the Python scripts ok, but it seems that the gtk main loop can't direct them correctly when they happen, causing segfaults like the log below.

I'm guessing it might have something to do with the python thread state not being initialized correctly, but not sure. Would anyone have insight on what I'm doing wrong or what to check?

#0  0xb7ecfc9a in PyFrame_New () from /usr/lib/libpython2.5.so.1.0
#1  0xb7f376ed in PyEval_EvalCodeEx () from /usr/lib/libpython2.5.so.1.0
#2  0xb7ed09b6 in ?? () from /usr/lib/libpython2.5.so.1.0
#3  0xb7eae327 in PyObject_Call () from /usr/lib/libpython2.5.so.1.0
#4  0xb7f30f7c in PyEval_CallObjectWithKeywords () from /usr/lib/libpython2.5.so.1.0
#5  0xb7eaeb5c in PyObject_CallObject () from /usr/lib/libpython2.5.so.1.0
#6  0xb424face in ?? () from /usr/lib/pymodules/python2.5/gtk-2.0/gobject/_gobject.so
#7  0xb76e113a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#8  0xb76f761d in ?? () from /usr/lib/libgobject-2.0.so.0
#9  0xb76f8bfc in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#10 0xb76f9076 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#11 0xb7ab3a8a in gtk_button_clicked () from /usr/lib/libgtk-x11-2.0.so.0
#12 0xb7ab5048 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#13 0xb76eecac in g_cclosure_marshal_VOID__VOID () from /usr/lib/libgobject-2.0.so.0
#14 0xb76df7a9 in ?? () from /usr/lib/libgobject-2.0.so.0
#15 0xb76e113a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#16 0xb76f6eba in ?? () from /usr/lib/libgobject-2.0.so.0
#17 0xb76f8bfc in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#18 0xb76f9076 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#19 0xb7ab3b2a in gtk_button_released () from /usr/lib/libgtk-x11-2.0.so.0
#20 0xb7ab3b73 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#21 0xb7b70e74 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#22 0xb76df7a9 in ?? () from /usr/lib/libgobject-2.0.so.0
#23 0xb76e113a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#24 0xb76f7266 in ?? () from /usr/lib/libgobject-2.0.so.0
#25 0xb76f8a7b in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#26 0xb76f9076 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#27 0xb7c9d156 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#28 0xb7b694cd in gtk_propagate_event () from /usr/lib/libgtk-x11-2.0.so.0
#29 0xb7b6a857 in gtk_main_do_event () from /usr/lib/libgtk-x11-2.0.so.0
#30 0xb79f3dda in ?? () from /usr/lib/libgdk-x11-2.0.so.0
#31 0xb7636305 in g_main_context_dispatch () from /lib/libglib-2.0.so.0
#32 0xb7639fe8 in ?? () from /lib/libglib-2.0.so.0
#33 0xb763a527 in g_main_loop_run () from /lib/libglib-2.0.so.0
#34 0xb7b6ae19 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0

Upvotes: 2

Views: 298

Answers (2)

asveikau
asveikau

Reputation: 40254

This looks like a pretty long stack, and some of the functions called look the same. (For example, g_signal_emit is called repeatedly from itself via g_closure_invoke.)

It looks to me like you may be causing a stack overflow, possibly by emitting a signal inside your callback that handles the signal, thus infinitely recursing until you run out of stack space and crash. This is just a guess and I don't know much about the GTK+/GLIB internals, but that is what it smells like to me. I'd put money on it. :-)

If not that, then maybe the GLIB closures are being chained too much (one callback calls another callback which calls another etc. etc. until you run out of space.) Maybe adjusting your stack size would help. (I don't remember offhand if there is an easy way to do this for main(), but if you're creating a new thread pthread_attr_setstacksize() might help.)

Upvotes: 0

Michael Dillon
Michael Dillon

Reputation: 32392

This is a situation where you are better off making one more Python interpreter and doing all your GTK work by sending Python snippets to that special GTK helper interpreter. That way your C code never uses GTK directly and you only need to worry about coordinating between the Python threads.

Upvotes: 0

Related Questions