Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2007
callbacks and threads problem


Message 1 in thread

Hi!

I have problems with strange crashes and ASSERT failures in qt. My
application is rather simple. Basically it is the helloworld QWidget where
I added a QTextEdit to the GUI. Further, the application uses an external
library (pjsip, a SIP stack) and registers a callback for logging. The SIP
stack itself starts a thread and if some events happen in the SIP stack my
callback function is triggered.
In this callback function I call the  -->append(logmessage) member of my
QTextEdit object.

At 50% of the time this works well and I see the log messages in the
QTextEdit. But the 50% I get strange crashes or ASSERT failures. The
ASSERT failure differ, e.g.:

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to
objects owned by a different thread. Current thread 81b9110. Receiver ''
(of type 'QTextDocument') was created in thread 8181330", file
kernel/qcoreapplication.cpp, line 269

or

ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
gp" in file text/qtextlayout.cpp, line 1493

or

ASSERT: "!isEmpty()" in file
../../include/QtCore/../../src/corelib/tools/qvector.h, line 234

Sometimes there are also other error/warning messages like:

QTextLayout::beginLayout: Called while already doing layout
QTextLayout::createLine: Called without layouting
QTextLayout::endLayout: Called without beginLayout()
ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
gp" in file text/qtextlayout.cpp, line 1493

or

QTextLayout::beginLayout: Called while already doing layout
QObject::startTimer: timers cannot be started from another thread

or

ASSERT: "data(f)->sizeDirty" in file text/qtextdocumentlayout.cpp, line 1943



I guess this might be caused by having another thread (created in the SIP
stack) will calls my callback function which then calls some qt function.

But how can I solve this? Are there special programming techniques needed
to handle multiple threads and callbacks?

thanks
Klaus

--
 [ signature omitted ] 

Message 2 in thread

Sounds like your SIP stack is spawning threads and calling your callback 
functions from them. Don't touch QWidgets from non-main threads. Use a 
Qt::QueuedConnection if you have to move signals from one thread to 
another, but *never* touch a QWidget from a non-main thread.

--Dave

klaus.mailinglists@xxxxxxxxx wrote:
> Hi!
>
> I have problems with strange crashes and ASSERT failures in qt. My
> application is rather simple. Basically it is the helloworld QWidget where
> I added a QTextEdit to the GUI. Further, the application uses an external
> library (pjsip, a SIP stack) and registers a callback for logging. The SIP
> stack itself starts a thread and if some events happen in the SIP stack my
> callback function is triggered.
> In this callback function I call the  -->append(logmessage) member of my
> QTextEdit object.
>
> At 50% of the time this works well and I see the log messages in the
> QTextEdit. But the 50% I get strange crashes or ASSERT failures. The
> ASSERT failure differ, e.g.:
>
> ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to
> objects owned by a different thread. Current thread 81b9110. Receiver ''
> (of type 'QTextDocument') was created in thread 8181330", file
> kernel/qcoreapplication.cpp, line 269
>
> or
>
> ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
> gp" in file text/qtextlayout.cpp, line 1493
>
> or
>
> ASSERT: "!isEmpty()" in file
> ../../include/QtCore/../../src/corelib/tools/qvector.h, line 234
>
> Sometimes there are also other error/warning messages like:
>
> QTextLayout::beginLayout: Called while already doing layout
> QTextLayout::createLine: Called without layouting
> QTextLayout::endLayout: Called without beginLayout()
> ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
> gp" in file text/qtextlayout.cpp, line 1493
>
> or
>
> QTextLayout::beginLayout: Called while already doing layout
> QObject::startTimer: timers cannot be started from another thread
>
> or
>
> ASSERT: "data(f)->sizeDirty" in file text/qtextdocumentlayout.cpp, line 1943
>
>
>
> I guess this might be caused by having another thread (created in the SIP
> stack) will calls my callback function which then calls some qt function.
>
> But how can I solve this? Are there special programming techniques needed
> to handle multiple threads and callbacks?
>
> thanks
> Klaus
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>   

--
 [ signature omitted ] 

Message 3 in thread

Dave Smith wrote:
> Sounds like your SIP stack is spawning threads and calling your callback 
> functions from them. Don't touch QWidgets from non-main threads. Use a 
> Qt::QueuedConnection if you have to move signals from one thread to 
> another, but *never* touch a QWidget from a non-main thread.

Thanks for the info - I didn't knew that.

I will try tomorrow and will report my results ...

regards
klaus

--
 [ signature omitted ] 

Message 4 in thread

Hi Dave!

Using Qt::QueuedConnection solved my problems - thanks!
Klaus


Dave Smith schrieb:
> Sounds like your SIP stack is spawning threads and calling your callback 
> functions from them. Don't touch QWidgets from non-main threads. Use a 
> Qt::QueuedConnection if you have to move signals from one thread to 
> another, but *never* touch a QWidget from a non-main thread.
> 
> --Dave
> 
> klaus.mailinglists@xxxxxxxxx wrote:
>> Hi!
>>
>> I have problems with strange crashes and ASSERT failures in qt. My
>> application is rather simple. Basically it is the helloworld QWidget 
>> where
>> I added a QTextEdit to the GUI. Further, the application uses an external
>> library (pjsip, a SIP stack) and registers a callback for logging. The 
>> SIP
>> stack itself starts a thread and if some events happen in the SIP 
>> stack my
>> callback function is triggered.
>> In this callback function I call the  -->append(logmessage) member of my
>> QTextEdit object.
>>
>> At 50% of the time this works well and I see the log messages in the
>> QTextEdit. But the 50% I get strange crashes or ASSERT failures. The
>> ASSERT failure differ, e.g.:
>>
>> ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to
>> objects owned by a different thread. Current thread 81b9110. Receiver ''
>> (of type 'QTextDocument') was created in thread 8181330", file
>> kernel/qcoreapplication.cpp, line 269
>>
>> or
>>
>> ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
>> gp" in file text/qtextlayout.cpp, line 1493
>>
>> or
>>
>> ASSERT: "!isEmpty()" in file
>> ../../include/QtCore/../../src/corelib/tools/qvector.h, line 234
>>
>> Sometimes there are also other error/warning messages like:
>>
>> QTextLayout::beginLayout: Called while already doing layout
>> QTextLayout::createLine: Called without layouting
>> QTextLayout::endLayout: Called without beginLayout()
>> ASSERT: "(pos == end && gp == current.num_glyphs) || logClusters[pos] ==
>> gp" in file text/qtextlayout.cpp, line 1493
>>
>> or
>>
>> QTextLayout::beginLayout: Called while already doing layout
>> QObject::startTimer: timers cannot be started from another thread
>>
>> or
>>
>> ASSERT: "data(f)->sizeDirty" in file text/qtextdocumentlayout.cpp, 
>> line 1943
>>
>>
>>
>> I guess this might be caused by having another thread (created in the SIP
>> stack) will calls my callback function which then calls some qt function.
>>
>> But how can I solve this? Are there special programming techniques needed
>> to handle multiple threads and callbacks?
>>
>> thanks
>> Klaus
>>
>> -- 
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>> "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>   
> 

--
 [ signature omitted ]