Results 1 to 5 of 5

Thread: QThread issues. Crash after 2058 runs.

  1. #1
    Join Date
    Oct 2009
    Posts
    5

    Wink QThread issues. Crash after 2058 runs.

    Following thread code runs 2058 times, after that it crashes. Can somebody help me figure out why? The idea of the program is create some class in main thread, pass it to worker thread, thread fills needed data and pass data back to main thread. This example crashes after 2058 runs, however it should go indefinately. I've run it 20 times, i've tried several computers, always the same number. In version of reduced qWarning() calls (print simple line each 100 runs) thread gets executed 3000 times. So I guess it does not depend on amount of qWarning() calls. And why pointer address for SharedData *d is always the same?

    main.cpp

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. TestThread* thread = new TestThread();
    6.  
    7. MainWindow w(thread);
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QThread>
    6. #include <QHash>
    7.  
    8. class SharedData
    9. {
    10. public:
    11. SharedData();
    12. QString var;
    13. QHash<QString, QString> hash;
    14. };
    15.  
    16. class TestThread : public QThread
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. TestThread(QObject *parent = 0);
    22. void doWork(SharedData* _data);
    23. void doCrash(QHash<QString, QString>* hash);
    24. signals:
    25. void completed(SharedData* d);
    26. private:
    27. SharedData* data;
    28. protected:
    29. void run();
    30. };
    31.  
    32. namespace Ui
    33. {
    34. class MainWindow;
    35. }
    36.  
    37. class MainWindow : public QMainWindow
    38. {
    39. Q_OBJECT
    40.  
    41. public:
    42. MainWindow(TestThread* t, QWidget *parent = 0);
    43. ~MainWindow();
    44. void runThread();
    45. public slots:
    46. void jobDone(SharedData* req);
    47.  
    48. private:
    49. Ui::MainWindow *ui;
    50. TestThread* t;
    51. int runcount;
    52. };
    53.  
    54. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug.h>
    4.  
    5. TestThread::TestThread(QObject *parent) : QThread(parent)
    6. {
    7. }
    8.  
    9. void TestThread::run()
    10. {
    11. qWarning() << "Thread running";
    12. data->var = "hello";
    13. doCrash(&data->hash);
    14. emit completed(data);
    15. }
    16.  
    17. void TestThread::doWork(SharedData* _data)
    18. {
    19. data = _data;
    20. qWarning() << "Attempting to start";
    21. if(!isRunning())
    22. {
    23. run();
    24. }
    25. else
    26. {
    27. qWarning() << "Oops. Already running";
    28. }
    29. }
    30.  
    31. void TestThread::doCrash(QHash<QString, QString>* hash)
    32. {
    33. hash->insert("test", "123");
    34.  
    35. /*
    36.   QHashIterator<QString, QString> i(*hash);
    37.   while (i.hasNext()) {
    38.   i.next();
    39.   qWarning() << i.key() + ":" + i.value();
    40.   }
    41.   */
    42. }
    43.  
    44. SharedData::SharedData()
    45. {
    46. }
    47.  
    48. void MainWindow::jobDone(SharedData* req)
    49. {
    50. qWarning() << "RETURNED";
    51. qWarning() << "var: " << req->var << " addr: " << &req->var;
    52. qWarning() << "cnt: " << req->hash.count() << " addr: " << &req->hash;
    53.  
    54. QHashIterator<QString, QString> i(req->hash);
    55.  
    56. while (i.hasNext()) {
    57. i.next();
    58. qWarning() << i.key() + ":" + i.value();
    59. }
    60.  
    61. delete req;
    62. runThread();
    63. }
    64.  
    65. MainWindow::MainWindow(TestThread* _t, QWidget *parent)
    66. : QMainWindow(parent), ui(new Ui::MainWindow)
    67. {
    68. ui->setupUi(this);
    69.  
    70. t = _t;
    71. connect(t, SIGNAL(completed(SharedData*)), this, SLOT(jobDone(SharedData*)));
    72. runcount = 0;
    73. runThread();
    74. }
    75.  
    76. void MainWindow::runThread()
    77. {
    78. SharedData* d = new SharedData();
    79. d->var = "test";
    80.  
    81. runcount++;
    82. qWarning() << "Run count: " << runcount;
    83.  
    84. qWarning() << "CREATING THREAD";
    85. qWarning() << "var: " << d->var << " addr: " << &d->var;
    86. qWarning() << "cnt: " << d->hash.count() << " addr: " << &d->hash;
    87.  
    88. t->doWork(d);
    89. }
    90.  
    91. MainWindow::~MainWindow()
    92. {
    93. delete ui;
    94. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QThread issues. Crash after 2058 runs.

    Your code seems to be endless loop, your call sequence goes something like:

    Qt Code:
    1. :
    2. :
    3. -> runThread
    4. -> doWork
    5. -> run
    6. -> emits completed (connected to jobDone)
    7. -> jobDone
    8. -> runThread
    9. -> doWork
    10. :
    11. :
    To copy to clipboard, switch view to plain text mode 

    Maybe because of this you reach some system resource limit (heap/stack) in this recursive function call sequence and that causes your code to crash at the point you mentioned.

    I have 8GB RAM in Ubuntu and I reach ~9500 (runcount) after crash, at that point my function call "tree" is 66609 function deep!

  3. #3
    Join Date
    Oct 2009
    Posts
    5

    Default Re: QThread issues. Crash after 2058 runs.

    Quote Originally Posted by tsp View Post
    Your code seems to be endless loop, your call sequence goes something like:

    Maybe because of this you reach some system resource limit (heap/stack) in this recursive function call sequence and that causes your code to crash at the point you mentioned.

    I have 8GB RAM in Ubuntu and I reach ~9500 (runcount) after crash, at that point my function call "tree" is 66609 function deep!
    It is intended to be ENDLESS. However it is not and I want to find out why.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread issues. Crash after 2058 runs.

    when it is intended to be endless it cannot be recursive (I did not investigate your code carefully but tsb might be right), because it will fill up your stack which ends in stack overflow error. And I think this happens in you case because I don't think that calling run() starts new thread (in docs in says that start() method is for starting thread which execute run() method in new thread - in OS meaning) and your emit expands to function call so it is endless recursion which requires endless stack size so it implies that you have to have endless amount of RAM. So buy some more RAM to extend life time of your app :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QThread issues. Crash after 2058 runs.

    Quote Originally Posted by zverj View Post
    It is intended to be ENDLESS. However it is not and I want to find out why.
    It is endless loop to the point where you run out of system resources. Please check the traces I posted above and you see the recursion, that is the problem in your code!

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.