<?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[[SOLVED] Access QObject subclass across threads]]></title><description><![CDATA[<p dir="auto">Hello everybody,</p>
<p dir="auto">It's been said really much about threads and QObjects and thier proper use but I still can't understand why I get  a crash on the following scenario:</p>
<p dir="auto">I have a QObject custom subclass:</p>
<p dir="auto">@class CustomObject : public QObject<br />
{<br />
Q_OBJECT<br />
public:<br />
void changeMyData(someDataType newValue)<br />
{<br />
myData = newValue;<br />
}</p>
<p dir="auto">public slots:<br />
void slot_changeMyAnotherData(someDataType newValue)<br />
{<br />
myAnotherData = newValue;<br />
}</p>
<p dir="auto">private:<br />
someDataType myData;<br />
someDataType myAnotherData;<br />
};@</p>
<p dir="auto">And I have two threads both accessing the same CustomObject instance: main thread calls <em>slot_changeMyAnotherData</em> and another thread calls <em>changeMyData</em>; the point is that they never access the same variables but this still produces a crash, why is that ? I cant get it... Could someone clarify this, please?<br />
Thanks in advance!</p>
]]></description><link>https://forum.qt.io/topic/30034/solved-access-qobject-subclass-across-threads</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 01:36:33 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/30034.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Jul 2013 13:28:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Tue, 30 Jul 2013 08:19:01 GMT]]></title><description><![CDATA[<p dir="auto">In general, using <em>reinterpret_cast</em> on data structures is a bad idea -- it makes it really hard to debug your program. I only use it to interpret byte streams.</p>
<p dir="auto">If you simply must cast, use <em>qobject_cast</em> for safety (or <em>dynamic_cast</em> for non-QObjects).</p>
]]></description><link>https://forum.qt.io/post/188496</link><guid isPermaLink="true">https://forum.qt.io/post/188496</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Tue, 30 Jul 2013 08:19:01 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Tue, 30 Jul 2013 07:49:50 GMT]]></title><description><![CDATA[<p dir="auto">You're welcome !</p>
<p dir="auto">So much casting makes me wonder, are you sure you're not preparing a can of worm ?</p>
]]></description><link>https://forum.qt.io/post/188489</link><guid isPermaLink="true">https://forum.qt.io/post/188489</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 30 Jul 2013 07:49:50 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Tue, 30 Jul 2013 06:35:54 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="SGaist" date="1375111273"]<br />
What the doc says is that a QObject has an additional rule, you have to take in account that the QObject might receive events any time (i.e. signal/slots interaction between objects living in different threads), so you might be writing to your field by calling your slot like a normal function and there might be an object in another thread that might emit the signal to modify the same variable. [/quote]<br />
That's exactly what I wanted to know, thanks!</p>
<p dir="auto">And You actually set me on the right path because after reading your comment I concentrated on some other aspects of my code rather than multithreading or QObjects and found the problem... I was reinterpret casting some multiple inherited subclass pointer to quin32 and then casting it back to the second base class pointer istead of casting it to subclass pointer first and then to the base class pointer :)<br />
Thanks a lot ! :)</p>
]]></description><link>https://forum.qt.io/post/188475</link><guid isPermaLink="true">https://forum.qt.io/post/188475</guid><dc:creator><![CDATA[terenty]]></dc:creator><pubDate>Tue, 30 Jul 2013 06:35:54 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Mon, 29 Jul 2013 15:21:13 GMT]]></title><description><![CDATA[<p dir="auto">Sorry I've hit the reply button to soon...</p>
<p dir="auto">What I was about to write was:</p>
<p dir="auto">Writing to a QObject is the same a writing to any other class (meaning that proper locking must be done or that you have a proper lock free implementation).</p>
<p dir="auto">What the doc says is that a QObject has an additional rule, you have to take in account that the QObject might receive events any time (i.e. signal/slots interaction between objects living in different threads), so you might be writing to your field by calling your slot like a normal function and there might be an object in another thread that might emit the signal to modify the same variable.</p>
<p dir="auto">Hope I didn't set you on the wrong path</p>
]]></description><link>https://forum.qt.io/post/188378</link><guid isPermaLink="true">https://forum.qt.io/post/188378</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 29 Jul 2013 15:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Mon, 29 Jul 2013 14:09:54 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="SGaist" date="1375105096"]Hi,</p>
<p dir="auto">Without some more code it's difficult to help.</p>
<p dir="auto">Technically your class looks ok but how do you access it ? Are you sure you don't have uninitialized pointers ? etc...[/quote]</p>
<p dir="auto">Hi,<br />
Thanks for the quick answer! From your words I understand that there's no mystical stuff going on with QObjects when accessing across threads and there should be no problems accessing <em>different</em> data fields of the <em>same</em> QObject <em>simultaniously</em> ?</p>
<p dir="auto">"The official documentation seems unclear to me :":<a href="http://harmattan-dev.nokia.com/docs/library/html/qt4/threads-qobject.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://harmattan-dev.nokia.com/docs/library/html/qt4/threads-qobject.html</a></p>
<p dir="auto">bq. If you are calling a function on an QObject subclass that doesn't live in the current thread and the object might receive events, you must protect all access to your QObject subclass's internal data with a mutex; otherwise, you may experience crashes or other undesired behavior.</p>
<p dir="auto">Sounds like all the data access must be synchronyzed even not overlapping...</p>
]]></description><link>https://forum.qt.io/post/188366</link><guid isPermaLink="true">https://forum.qt.io/post/188366</guid><dc:creator><![CDATA[terenty]]></dc:creator><pubDate>Mon, 29 Jul 2013 14:09:54 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Access QObject subclass across threads on Mon, 29 Jul 2013 13:38:16 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Without some more code it's difficult to help.</p>
<p dir="auto">Technically your class looks ok but how do you access it ? Are you sure you don't have uninitialized pointers ? etc...</p>
<p dir="auto">EDIT:incomplete answer</p>
]]></description><link>https://forum.qt.io/post/188354</link><guid isPermaLink="true">https://forum.qt.io/post/188354</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 29 Jul 2013 13:38:16 GMT</pubDate></item></channel></rss>