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