Qt Creator crashes when using multiple threads <p>I'm writing a Qt (5.3) program which has a joystick test UI in it, but I need a separate thread for an infinite while loop looking for joystick axis/button value changes through SDL. That part of the code is working fine as I can have the thread qDebug() messages and it seems to work. But from the main window, when I try to open the test joystick UI, the program crashes. I've had the test joystick UI running separation WITHOUT the JoystickThread thread and it seems to open up fine. </p> <p>The error messages are inconsistent though - some times, I just get</p> <blockquote> <p>The program has unexpectedly finished. /home/narendran/QtWorkspace/build-LinkControl-Desktop-Debug/LinkControl crashed</p> </blockquote> <p>This has shown up once:</p> <blockquote> <p>QXcbWindow: Unhandled client message: "_GTK_LOAD_ICONTHEMES" </p> </blockquote> <p>And a few other times:</p> <blockquote> <p>[xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. star: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.</p> </blockquote> <p>I found that this was common if XInitThreads(); is not run in the main function, but even with it on there, it crashes with the same error(s).</p> <p><strong>main.cpp</strong></p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;qsplashscreen.h&gt; #include "linkcontrol.h" #include "configure.h" #include &lt;unistd.h&gt; #include &lt;QApplication&gt; #include &lt;QPixmap&gt; #include &lt;QStyle&gt; #include &lt;QDesktopWidget&gt; #include "linkports.h" #include "joystickthread.h" #include &lt;X11/Xlib.h&gt; int main(int argc, char *argv[]) { XInitThreads(); QApplication a(argc, argv); QPixmap pix(":splash.png"); QSplashScreen splash(pix); splash.show(); a.processEvents(); JoystickThread jsThread; jsThread.start(); LinkControl linkcontrol; usleep(1000000); splash.finish(&amp;linkcontrol); usleep(100000); linkcontrol.show(); linkcontrol.setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,linkcontrol.size(),a.desktop()-&gt;availableGeometry())); return a.exec(); } </code></pre> <p>The actual thread is in <strong>joystickthread.cpp</strong></p> <pre class="lang-cpp prettyprint-override"><code>#include "joystickthread.h" #include "global.h" #include "unistd.h" /* JoystickThread::JoystickThread(int _interval) { this-&gt;interval_us = _interval; } */ void JoystickThread::run() { while(1) { if(joystick-&gt;connected) { joystick-&gt;updateJSData(); // Check for changed values for(int i=0; i&lt;joystick-&gt;axis.count(); i++) { if(joystick-&gt;axis.value(i) != joystick-&gt;axis_last[i]) { joystick-&gt;axisUpdateEmit(i); // qDebug() &lt;&lt; "AXIS: " &lt;&lt; i &lt;&lt; "\tVALUE: " &lt;&lt; joystick-&gt;axis.value(i); } joystick-&gt;axis_last[i] = joystick-&gt;axis.value(i); } for(int i=0; i&lt;joystick-&gt;button.count(); i++) { if(joystick-&gt;button.value(i) != joystick-&gt;button_last[i]) { joystick-&gt;btnUpdateEmit(i); // qDebug() &lt;&lt; "BUTTON: " &lt;&lt; i &lt;&lt; "\tVALUE: " &lt;&lt; joystick-&gt;button.value(i); } joystick-&gt;button_last[i] = joystick-&gt;button.value(i); } } usleep(2500); } } </code></pre> <p>The function that causes the program to crash is in <strong>linkcontrol.cpp</strong></p> <pre class="lang-cpp prettyprint-override"><code>void LinkControl::on_actionJoystick_Test_triggered() { qDebug() &lt;&lt; "STARTING CHECK"; if(!js_test-&gt;initialized) { qDebug() &lt;&lt; "NOT INIT"; js_test = new TestJoystick(); js_test-&gt;initialized = true; qDebug() &lt;&lt; "FININSH INIT"; } if(joystick-&gt;connected) { qDebug() &lt;&lt; "SHOWING UI"; js_test-&gt;show(); } else { QMessageBox::critical(this, tr("No Joystick Connected!"), tr("Please connect a joystick first...")); } } </code></pre> <p>Where <em>js_test</em> is declared as a <em>TestJoystick</em> object in the <strong>linkcontrol.h</strong> file</p> <pre class="lang-cpp prettyprint-override"><code>public: explicit LinkControl(QWidget *parent = 0); QSlider *portSliders[16]; QLineEdit *setVals[16]; SerialTerminal *ser_term; TestJoystick *js_test; ~LinkControl(); </code></pre> <p>Thank you very much! Please let me know if you need anymore information.</p>