Hey guys,

Im making a gui with workerthreads to do the work. My gui mainwindow generates the threads, assigns my worker object, connects the thread and the worker events. Doing this i want to pass a var that stands for the unique thread id to split up the work between threads.

Qt Code:
  1. void MainWindow::on_pushButton_2_clicked()
  2. {
  3. for (int id=1;id<=2;id++)
  4. {
  5.  
  6. int *thread_id_on_heap_p =new int(id);
  7.  
  8. QThread *thread = new QThread;
  9.  
  10. Worker *worker = new Worker(thread_id_on_heap_p);
  11.  
  12. worker->moveToThread(thread);
  13.  
  14. //connect events from worker<-> threads
  15. connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
  16. connect(thread, SIGNAL(started()), worker, SLOT(testworker()));
  17. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
  18. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
  19. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
  20.  
  21. thread->start();
  22. }
  23.  
  24. }
To copy to clipboard, switch view to plain text mode 

this is the header for the worker object:

Qt Code:
  1. class Worker : public QObject {
  2. Q_OBJECT
  3.  
  4. public:
  5. Worker(int* core);
  6. ~Worker();
  7.  
  8. public slots:
  9. void testworker();
  10.  
  11. signals:
  12. void finished();
  13. void error(QString err);
  14. //method to send info from worker thread to textbrowser in gui
  15. void update_textbrowser(QString info);
  16.  
  17. private:
  18. int thread_id;
  19. };
To copy to clipboard, switch view to plain text mode 

this is the constructor of the worker object where i copy the id to the object :

Qt Code:
  1. Worker::Worker( int *core) {
  2.  
  3. this->thread_id = *core;
  4.  
  5. qDebug () << "constructor " + QString::number(this->thread_id);
  6. }
To copy to clipboard, switch view to plain text mode 

after this i just use this->thread_id in my other worker functions, it seems to be working.

So my question is this a save way to pass the thread id? Will when on_PushBotton2_clicked() ends the variable will still be available since i passed the adress of the var? or is that adressspace free to be written on by other code? am i creating a memory leak here because i dont delete the data the pointer "thread_id_on_heap_p" points to? Are there better approaches for this problem?

be gentle guys