Before to describe the patch , I would like to know how many people are using Linux as the platform and also encountered problem in using OSC which hangs / segfault frequently? I can hardly find a report in this forum for the problem. I just afraid it is my own system problem until it was confirmed from IRC channel.
OSC sometimes crashes on start-up , or few minutes after execute. The exact error message and place for segfault occur are random. By reading the source code of touchlib and OpenCV, I found that it is caused by a race condition on the use of cvWaitKey and cvShowImage (in filter.cpp).
Although both of the function has acquired mutex lock for multi-threading programming , the mutex lock is different!
cvWaitKey use last_key_mutex while cvShowImage use window_mutex. OSC spins a thread to read camera image , process and show the image through cvShowImage in Filter::process() . And then the original thread calls cvWaitKey to read the next key and run Gtk+ main loop. Therefore, it is possible to have two threads to entry the critical session in the same time ,such that a race condition may happen.
To avoid both of the thread to access the critical session of GDK, I have added few gdk_threads_enter()/gdk_threads_leave() in filter.cpp , and GThread initializatoin in osc.cpp. Then the osc could lives more than 30 minutes in my system.
However, I don’t think it is a good method to solve the problem. It is better to have extra mutex lock like HighGuiMutexLock.
So I just made this patch as a test to proof the source of error. It is incomplete , and will break the compilation in windows platform.
Could somebody take the patch to test and report the result?
Command to apply the patch:
patch -p0 < touchlib-linux-race-condition.patch