Results 1 to 6 of 6

Thread: without Qt::QueuedConnection will crash

  1. #1
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default without Qt::QueuedConnection will crash

    Hi, All

    I do a testing which use a work thread to access a variable (here is str) which belong to main thread. I found if I add QueuedConnection in connect function ,it will not crash,but without it will crash .

    Does anybody know why?

    Thanks advance for your help.

    Qt Code:
    1. #ifndef AAA_
    2. #define AAA_
    3. #include <QThread>
    4. #include <QString>
    5. #include <QDebug>
    6. class myClass : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. myClass ()
    11. {
    12. connect(this,
    13. SIGNAL(startSignal()),
    14. this, SLOT(onDoComm())); //will crash
    15. //,Qt::QueuedConnection); //add this parameter will not
    16. }
    17. void run() { exec(); }
    18. void startComm() {emit startSignal();}
    19. signals:
    20. void startSignal();
    21. public slots:
    22. void onDoComm()
    23. {
    24. static long i=1;
    25. qDebug()<<str;
    26. str=QString("%1%2").arg(i++);
    27. emit startSignal();
    28. };
    29. private:
    30. QString str;
    31. };
    32.  
    33.  
    34. #endif
    35.  
    36.  
    37.  
    38. #include <iostream>
    39. #include <QApplication>
    40. #include "aaa.h"
    41.  
    42. using namespace std;
    43. int main(int argc ,char *argv[])
    44. {
    45. QApplication app(argc, argv);
    46.  
    47. myClass a;
    48. a.start();
    49. a.startComm();
    50. app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: without Qt::QueuedConnection will crash

    Because without Qt::QueuedConnection emmiting signal is like a calling slot function. So You call onDoComm() BEFORE ending this method that is to say "classic stack overflow".
    With Qt::QueuedConnection slot function is calling by signal dispatcher in idle time.

    Question is : why You emit StartSignal() in onDoComm() ?

  4. The following user says thank you to Lesiok for this useful post:

    hashb (1st December 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: without Qt::QueuedConnection will crash

    Here the big problem is that you're dealing with two distinct event loops.
    Even with a queued connection, the logic is still wrong as the slot of the thread is not executed in the thread itself but in the main thread resulting in possible crashes.

    Edit: never mind, the connection is made in the main thread. Just ignore this post.

  6. The following user says thank you to tbscope for this useful post:

    hashb (1st December 2010)

  7. #5
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: without Qt::QueuedConnection will crash

    Hi tbscope,you mean the slot of the thread executed in the main thread ?


    Quote Originally Posted by tbscope View Post
    Here the big problem is that you're dealing with two distinct event loops.
    Even with a queued connection, the logic is still wrong as the slot of the thread is not executed in the thread itself but in the main thread resulting in possible crashes.

    Edit: never mind, the connection is made in the main thread. Just ignore this post.
    Thanks a lot for you explanation,
    it was just a test in order to produce a loop

    Quote Originally Posted by Lesiok View Post
    Because without Qt::QueuedConnection emmiting signal is like a calling slot function. So You call onDoComm() BEFORE ending this method that is to say "classic stack overflow".
    With Qt::QueuedConnection slot function is calling by signal dispatcher in idle time.

    Question is : why You emit StartSignal() in onDoComm() ?
    Last edited by hashb; 1st December 2010 at 07:05.

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: without Qt::QueuedConnection will crash

    Quote Originally Posted by hashb View Post
    Hi tbscope,you mean the slot of the thread executed in the main thread ?




    Thanks a lot for you explanation,
    it was just a test in order to produce a loop
    Do this with QTimer. Something like this :
    Qt Code:
    1. #ifndef AAA_
    2. #define AAA_
    3. #include <QThread>
    4. #include <QString>
    5. #include <QDebug>
    6. #include <QTimer>
    7.  
    8. class myClass : public QThread
    9. {
    10. Q_OBJECT
    11. public:
    12. myClass ()
    13. {
    14. connect(&timer,
    15. SIGNAL(timeout()),
    16. this, SLOT(onDoComm()));
    17. }
    18. void run() { exec(); }
    19. void startComm() {timer.start(10);}
    20. signals:
    21. void startSignal();
    22. public slots:
    23. void onDoComm()
    24. {
    25. static long i=1;
    26. qDebug()<<str;
    27. str=QString("%1%2").arg(i++);
    28. };
    29. private:
    30. QString str;
    31. QTimer timer;
    32. };
    33. #endif
    To copy to clipboard, switch view to plain text mode 
    In this example onDoComm will be executed one time per 10 ms

Similar Threads

  1. QueuedConnection pointer problem
    By cafu in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2010, 13:24
  2. Replies: 1
    Last Post: 6th November 2009, 18:33
  3. Qt::QueuedConnection and passing by Reference
    By soul_rebel in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2007, 18:47
  4. How to use Qt::QueuedConnection?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 08:04
  5. QueuedConnection's and threads
    By gri in forum Qt Programming
    Replies: 14
    Last Post: 24th January 2007, 09:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.