Hi Everyone,
I am experiencing a very weird problem with my wxPython application that I
believe is related to the internals of wxPython/wxWindows and the main
event thread. The situation is as follows (Windows 2000, Python 2.2.2,
wxPython 2.4.0.1):
My main application talks to a python-wrapped .dll which is a low-level
serial driver talking to a serial device. We have currently added a new
protocol which talks to a wireless device using a third-party .dll via COM.
Our .dll does a CoInitialize(NULL) to talk to the device in a
single-threaded apartment arrangement. Well, inside my application there
are basically two cases involved:
1) I need to do a long operation, so I fire off another thread to do the
serial operation, waiting in my main thread to be informed when it is
complete.
2) I need to execute a simple, serial command which will succeed or fail
immediately, so I just call it inline from the main wxApp thread, from
within a menu event handler, for instance.
Well, as it turns out, I am getting sporadic behaviour with my application
crashing sometimes but only when calling the simple functions from the main
thread. I am also seeing cases where I will call a simple command a few
times (from the main thread), and then try to execute a normal, purely
wxPython event handler and my application will simply vanish. No traceback,
no violent crash - it simply vanishes. So, for instance, I will execute a
quick serial operation a couple times (from the main thread), then goto my
File..Open.. menu (which simply brings up a wxFileDialog) - and the
application will disappear. Neither of these crashes happen very often,
and sometimes I have to try quite a few times to repeat the error. Also,
it only happens with the new device we have added which uses COM.
I cannot make the application crash with any of the commands which have
been wrapped to be executed in a separate thread. Similarly, if I add some
code to execute the simple commands in separate threads rather than the
main thread, the problem seems to go away as well. This is leading me to
believe that there is something funky going on with calling COM-based stuff
from the main thread of a wxApp.
Can anyone see why this may (or may not) be the case? I am not a COM
expert, but I have done quite a lot with wxPython, and this one is driving
me crazy. I am trying to minimize this to a simple case to confirm it is
not something weird in my app, but I am fairly confident there isn't.
BTW - I have tried making a multithreaded .dll using CoInitializeEx, then
importing pythoncom to force my application to advertise itself as being
multi-threaded as well, and the problem seems to go away in this case too.
I can then call the serial functions from the main thread, or from child
threads and I cannot make it crash. This further points to a threading
thing in my opinion...
Any help or thoughts would be appreciated.
Thanks,
Mark.