<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Application crashes on QThread::quit()]]></title><description><![CDATA[<p dir="auto">Hey Gurus!</p>
<p dir="auto">I have a QObject class which I launch in a  QThread. Everything seems to be working fine until I try to QThread::quit() the thread, at which point my application crashes with an:<br />
<strong>ASSERT failure in QMutexLocker: "QMutex pointer is misaligned", file thread\qmutex.h, line 196</strong><br />
Whilst I have been developing code in Visual Studio for many years, I'm quite new to QT (which I'm loving BTW!). To be honest, I'm not sure how to quit the thread and close my QObject class correctly. My code is as follows:</p>
<p dir="auto">Header of my QObject Class:</p>
<pre><code>#ifndef UPNPDISCOVERTHREAD_H
#define UPNPDISCOVERTHREAD_H

#include "../upnp/avcontrol.hpp"
#include "../upnp/actioninfo.hpp"

#include &lt;QObject&gt;
#include &lt;QThread&gt;
#include &lt;QDebug&gt;

namespace upnp
{
 class CControlPoint;
};

USING_UPNP_NAMESPACE

class CUpnpDiscoverThread : public QObject
{
    Q_OBJECT
public:
    explicit CUpnpDiscoverThread(int numSwitches, QObject *parent = nullptr);
    ~CUpnpDiscoverThread();
    void start(QThread &amp;thread);

signals:
    /*! Emitted when a all switches are found. */
    void scanComplete(QVarLengthArray&lt;struct WemoSwitch *&gt; *ws);

protected slots:
    void newDevice (QString const &amp; uuid);
    void upnpError (int errorCode, QString const &amp; errorString);
    void networkError (QString const &amp; deviceUUID, QNetworkReply::NetworkError errorCode, QString const &amp; errorString);
    void isScanFinished();

public slots:
    void startDiscovery();
    void quit();

private:
    CControlPoint* m_cp = nullptr; //!&lt; It is the CControlPoint.
    QList&lt;CControlPoint::TArgValue&gt; m_args;
    int GetSignalStrength( struct WemoSwitch *ws );
    int m_switchCount; // Because I don't know when the network scan is complete, I count the number of switches discovered every X secs, if it hasn't changed in Y secs, then I assume the scan is complete
    int m_retrySwitchCount; // The number of times to retry the switch count before timing out.
    int m_numSwitches; // The number of scanned switches expected to be found
    QVarLengthArray&lt;struct WemoSwitch *&gt; m_wemoSwitches;
};

#endif // UPNPDISCOVERTHREAD_H
</code></pre>
<p dir="auto">QObject CPP (part of):</p>
<pre><code>#include "upnpdiscoverthread.h"
#include "globals.h"

#include &lt;QDir&gt;
#include &lt;QSettings&gt;
#include &lt;QtXml&gt;
#include &lt;QMessageBox&gt;
#include &lt;QDebug&gt;

CUpnpDiscoverThread::CUpnpDiscoverThread(int numSwitches, QObject *parent) : m_numSwitches( numSwitches), QObject(parent)
{
}

CUpnpDiscoverThread::~CUpnpDiscoverThread()
{
    if ( m_cp )
    {
        m_cp-&gt;close();

        delete m_cp;
    }
}

void CUpnpDiscoverThread::start(QThread &amp;thread)
{
    connect( &amp;thread, SIGNAL(started()), this, SLOT(startDiscovery()));
    connect( &amp;thread, SIGNAL(finished()), this, SLOT(quit()));
}

void CUpnpDiscoverThread::startDiscovery()
{
    qDebug() &lt;&lt; "Starting discovery";

   ...
}

void CUpnpDiscoverThread::quit()
{
    quit();
}

void CUpnpDiscoverThread::isScanFinished()
{
      emit scanComplete( &amp;m_wemoSwitches);
}
</code></pre>
<p dir="auto">Main window construction and execution of thread:</p>
<pre><code>CUpnpDiscoverThread *m_discover = new CUpnpDiscoverThread( count );

QThread *m_discoverThread = new QThread( this );

m_discover-&gt;start( *m_discoverThread );

m_discover-&gt;moveToThread( m_discoverThread );

m_discoverThread-&gt;start();

connect( m_discover, &amp;CUpnpDiscoverThread::scanComplete, this, &amp;MainWindow::scanComplete );
</code></pre>
<p dir="auto">Main window termination of thread (called in the scanComplete SLOT):</p>
<pre><code>m_discoverThread-&gt;quit();

delete m_discoverThread;

delete m_discover;
</code></pre>
<ul>
<li>
<p dir="auto">Could someone please shed some light on what I am doing wrong, and how to correctly terminate my thread and class?</p>
</li>
<li>
<p dir="auto">I assumed that the QThread::quit() would send a finished SIGNAL to my QObject?</p>
</li>
<li>
<p dir="auto">I assume it is up to me to delete the QThread and QObject?</p>
</li>
</ul>
<p dir="auto">Thanks heaps in advance.</p>
<p dir="auto">Steve Q. :-)</p>
]]></description><link>https://forum.qt.io/topic/90553/application-crashes-on-qthread-quit</link><generator>RSS for Node</generator><lastBuildDate>Thu, 11 Jun 2026 03:30:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/90553.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 09 May 2018 08:21:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 12:11:43 GMT]]></title><description><![CDATA[<p dir="auto">Thanks again Sierdzio, as expected, this has fixed it!</p>
<p dir="auto">-SQ :-)</p>
]]></description><link>https://forum.qt.io/post/457162</link><guid isPermaLink="true">https://forum.qt.io/post/457162</guid><dc:creator><![CDATA[steveq]]></dc:creator><pubDate>Wed, 09 May 2018 12:11:43 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 10:52:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/steveq">@<bdi>steveq</bdi></a> said in <a href="/post/457135">Application crashes on QThread::quit()</a>:</p>
<blockquote>
<p dir="auto">Thanks so much Sierdzio, I feel like a goose!</p>
</blockquote>
<p dir="auto">heh, no need to feel bad. I guess every programmer could share a story where they missed a single semicolon and kept debugging for 2 days :D That just happens sometimes.</p>
]]></description><link>https://forum.qt.io/post/457140</link><guid isPermaLink="true">https://forum.qt.io/post/457140</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 09 May 2018 10:52:40 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 10:36:29 GMT]]></title><description><![CDATA[<p dir="auto">OMG I can't believe I didn't see this!!</p>
<p dir="auto">What a silly mistake.</p>
<p dir="auto">Thanks so much Sierdzio, I feel like a goose!</p>
<p dir="auto">I guess sometimes you look too hard, and it takes a second pair of eyes.</p>
<p dir="auto">I'll fix this soon and let you know.</p>
<p dir="auto">Thanks heaps once again,</p>
<p dir="auto">-SQ :-)</p>
]]></description><link>https://forum.qt.io/post/457135</link><guid isPermaLink="true">https://forum.qt.io/post/457135</guid><dc:creator><![CDATA[steveq]]></dc:creator><pubDate>Wed, 09 May 2018 10:36:29 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 09:34:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/steveq">@<bdi>steveq</bdi></a> said in <a href="/post/457104">Application crashes on QThread::quit()</a>:</p>
<blockquote>
<p dir="auto">QThread *m_discoverThread = new QThread( this );</p>
</blockquote>
<p dir="auto">hmmmm... you are creating a local variable here, right? If yes, then <em>what</em> you call quit() on?</p>
<p dir="auto">I think your <code>m_discoverThread</code> (class member) is still a nullptr! So you should change that to:</p>
<pre><code>m_discoverThread = new QThread( this );
</code></pre>
]]></description><link>https://forum.qt.io/post/457123</link><guid isPermaLink="true">https://forum.qt.io/post/457123</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 09 May 2018 09:34:02 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 09:26:01 GMT]]></title><description><![CDATA[<p dir="auto">Hi Sierdzio,</p>
<p dir="auto">With regards to the infinite loop. What can I say... oops! Lol</p>
<p dir="auto">I have tried your suggested changes (and fixed the infinite loop!), but the code never arrives at my break points.</p>
<p dir="auto">My app crashes on the m_discoverThread-&gt;quit(); line!</p>
<p dir="auto">I'm at a bit of a loss as to what to do.</p>
<p dir="auto">Thanks,</p>
<p dir="auto">SQ</p>
]]></description><link>https://forum.qt.io/post/457120</link><guid isPermaLink="true">https://forum.qt.io/post/457120</guid><dc:creator><![CDATA[steveq]]></dc:creator><pubDate>Wed, 09 May 2018 09:26:01 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes on QThread::quit() on Wed, 09 May 2018 08:44:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/steveq">@<bdi>steveq</bdi></a> said in <a href="/post/457104">Application crashes on QThread::quit()</a>:</p>
<blockquote>
<p dir="auto">void CUpnpDiscoverThread::quit()<br />
{<br />
quit();<br />
}</p>
</blockquote>
<p dir="auto">That's an infinite loop :-)</p>
<blockquote>
<p dir="auto">m_discover-&gt;start( *m_discoverThread );<br />
m_discover-&gt;moveToThread( m_discoverThread );</p>
</blockquote>
<p dir="auto">You should connect the signals and slots <strong>after</strong> moving the object to thread.</p>
<blockquote>
<p dir="auto">m_discoverThread-&gt;quit();</p>
</blockquote>
<p dir="auto">You should also call <code>m_discoverThread-&gt;wait();</code></p>
<blockquote>
<p dir="auto">delete m_discover;</p>
</blockquote>
<p dir="auto">A nicer way is to connect thread's finished() signal to m_discover's deleteLater() slot.</p>
<p dir="auto">Hope these hints help. I have not analysed the whole code.</p>
]]></description><link>https://forum.qt.io/post/457110</link><guid isPermaLink="true">https://forum.qt.io/post/457110</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 09 May 2018 08:44:14 GMT</pubDate></item></channel></rss>