Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

Very Strange Program Crashes

over 7 years ago

So I ran into a problem with my code that was causing crashes that gave no error messages. This was very frustrating because I had no idea where to start actually looking up info on what might be causing the crash.

Here's what was happening: My 'logbox,' which is where the log text is displayed as it gets processed, seemed to be causing crashes when particularly large log files (10k+) were opened. I did not actually test this much on medium-sized logfiles, but kept using the 10k file in my tests because it was really good at producing the crash. This crash sometimes happened and sometimes didn't happen.

The code that actually processes the log lines is executed in a separate thread, which then tells the GUI to display the line after it has been processed.

A couple days passed without me doing much work - I was discouraged and worried that tkinter may just have limitations that won't allow logfiles that big to work with the program.

I randomly did a search on 'tkinter limitations' and got this page, which had a few interesting points. But the jackpot was in section 10.7.1...

"If you do use threads in Tkinter programs, however, only the main thread (the one that built the GUI and started the mainloop) can make GUI calls. Things like the update method described in the preceding section cannot be called from spawned threads in a GUI programthey'll likely trigger very strange program crashes."

A-HA!

Time to restructure part of my app... Again :P

Comments

over 7 years ago (08 Jul 2009 at 03:16 PM) by Stou S.
Ah yes... welcome to multi-threaded programming. You should be able to easily get around this limitation by using a thread-safe double ended queue that the logger thread will push log-lines to... and the GUI can read from. Then you can use your GUIs message interface (Events in wxWidgets or slots in Qt) to notify the GUI that there are log lines waiting in the queue. In fact you should be able to just use a python list guarded by critical sections or a mutex.

Also, and I am sure you've heard this before, think really really hard for alternatives before using threads. Threads are great... but it's also very easy to get random deadlocks or crashes... that are difficult to debug.
updated over 7 years ago (08 Jul 2009 at 07:04 PM) by Jenifer Irwin
Yeah, I didn't know extra threads could make a program so finicky! I think I'm just going to use an .after method in the GUI itself that calls the processing function. That's what I was doing originally but I felt that it was 'cluttering' the GUI class too much. I'm a little bit too anal about trying to make my programs and classes 'clean,' heh.