How to Pause/stop all threads when one thread has SEG.FAULT
-
Hi All,
I have a Qt application which uses two threads(UI and Background). The application executes scripts for testing. the UI has a Tableview which is dynamically populated and background thread runs the scripts and provides necessary data to UI. Sometimes the UI thread panics in PaintEvent function. I suspect that the background thread is not giving proper data sometimes. To check the same i tried seeing the backtrace for the background thread but found that it was still running. since the segfault happens randomly and at different places everytime, it is difficult ot debug the issue. Can someone help me how to stop the background thread when UI thread is stopped? -
Hi,
See http://stackoverflow.com/questions/7376228/linux-c-catching-kill-signal-for-graceful-termination for handling segfaults.
[quote]the UI has a Tableview which is dynamically populated and background thread runs the scripts and provides necessary data to UI.[/quote]How do you update your table view? The table view is not thread-safe -- it can only be updated from the UI thread. Your background thread is not allowed to update it.
-
If you suspect is on background thread, we need to kill the culprit itself. I suggest you to start dumping the result of background thread in to a file and inspect. You said that data from bgthread is passed to ui thread using sig/slot. You should be able to print and see the data in slot as well. Not sure how big is the data too inspect.
You can try the following as well if
- Get the current thread QThread::currentThread
- Connect finished() signal to slot - Not sure signal emits if thread crashes.
- Inside the slot, terminate other thread.
This works only if the finished signal is emitted.
-
[quote author="sathishm" date="1405665571"]Whenever I need to update the table i will emit signal from bgthread and UI thread will update the same.[/quote]Ok, that's the correct way to do it. (You did use AutoConnection or QueuedConnection, right?)
I recommend using tools like Valgrind and Helgrind to help with debugging. They are designed specifically to detect memory errors and thread access errors.
-
Hi, Thanks for the reply. First i will try Printing the data into a file(not sure if this will work as there are too many.)
I will run Valgrind after this to check for any suspicious things happening.Also i have another doubt, There is one place in BgThread where i do the following :
@
tblview->scrollTo(startcolindex,QAbstractionItemView::PostionAtCenter);
tblview->SelectionModel()->select(QItemSelection(startcolindex,endcolindex,QItemSelectionmodel::Rows));
@Is this the wrong way, Should i use Signal and Slot mechanism to do this also?
The reason why i suspect this is, it is the only place in BGthread where i change Qtable property.
I am currently trying to run application commentiing the above lines. Will see if i still get any seg.faults.
-
I'm assuming that you are passing the tblview to thread and using it. I don't see issue in code. However it is suspect as well.
I suggest you better use the signal/slot mechanism for update in the UI.
-
[quote author="sathishm" date="1405680812"]Also i have another doubt, There is one place in BgThread where i do the following :
@
tblview->scrollTo(startcolindex,QAbstractionItemView::PostionAtCenter);
tblview->SelectionModel()->select(QItemSelection(startcolindex,endcolindex,QItemSelectionmodel::Rows));
@Is this the wrong way, Should i use Signal and Slot mechanism to do this also?
The reason why i suspect this is, it is the only place in BGthread where i change Qtable property.[/quote]Your suspicion is correct. You must not call any functions of GUI classes in a background thread. This will cause crashes.
See this documentation: http://qt-project.org/doc/qt-5/threads-qobject.html It says "GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread."