1

I am newbee at QThreads and a problem that I am suspicious of deleting the objects when the program finishes.My program has a class that derived from QObject:

class My_application: public QCoreApplication{
   ....
   ....
};

class My_Class: public QObject{
  ...
  ...
}; 

void My_Class::process{

     QTimer timer=new QTimer();
     timer->setInterval(time);
     connect(timer,SIGNAL(timeout()),this,SLOT(dowork()));
     timer->start(); 

} 

 My_application::My_application:QCoreApplication{

    my_class=new My_Class();

    QThread thread=new QThread();

    my_class->moveToThread(thread);

    connect(thread,SIGNAL(started()),my_class,SLOT(process())) ;

    connect(my_class,SIGNAL(finished()),thread,SLOT(quit())) ;

    connect(thread,SIGNAL(finished()),thread,SLOT(deletelater())) ;

    connect(my_class,SIGNAL(finished()),my_class,SLOT(deletelater())) ;        


 }

   void My_Class::dowork(){

  //here doing the work with timer elapsed.Doing work with some buffer and send data 
  //               

  }

If I stop my program I see that some objects are not deleted correctly and my program does not work when I restart it.Actually I am not so familiar with Qt threads and i wonder when does the destructor of My_Class will be called? and am i doing the wrong thing?

user376507
  • 2,004
  • 1
  • 19
  • 31
barp
  • 6,489
  • 9
  • 30
  • 37
  • Not sure but you can use deleteLater() with your thread connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater())); and be cautious with deleteLater() with this post as reference . http://stackoverflow.com/questions/9632989/qobjectdeletelater-across-a-qthread – Wagmare Feb 28 '14 at 09:10
  • Does you class contain 'finished' signal? Where do you emit it? – Dmitry Sazonov Feb 28 '14 at 09:23
  • Well, at least [`QThread::finished()` signal](https://qt-project.org/doc/qt-5.0/qtcore/qthread.html#finished) connected to `deleteLater()` is the canonical way to delete objects when their thread finishes, so you're doing that right, I'd remove the other `deleteLater()` connection just to keep things tidy. – hyde Feb 28 '14 at 16:46
  • How do you stop your program? Also, why did you subclass QCoreApplication? – JKSH Mar 01 '14 at 03:50

1 Answers1

0

You forgot to control lifetime of your timer (in My_Class::process):

connect( this, SIGNAL( destroyed() ), timer, SLOT( deleteLater() ) );

or QTimer timer=new QTimer( **this** );

You should delete my_class=new My_Class(); manually. It will not be deleted on finished signal of thread, because there will be no event loop to process deleteLater slot.

I'm confused, why you don't want to declare my_class as member of My_application ?

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • @dimitry you are right i was trying to run these in a loop so i forgot to call emitFinished.SO there is another question.If I call emit finished and clear the objects.And then continue to loop does it run second time correctly?I want to clear the objects and run everything again wşthout closing the program – barp Feb 28 '14 at 09:43
  • So I want when I close my_program ,.ı want my application to send finisded to thread and thread to send finished to my_class and my_class to destroy objects – barp Feb 28 '14 at 09:50
  • Your applicatioin will send `aboutToClose`. On this signal you should stop all threads, wait for real stop and destroy objects. It is not necessary to implement all logic with voodooing signals and slots. – Dmitry Sazonov Feb 28 '14 at 12:05
  • 1
    Why don't you make `timer` a member of the `My_Class`. In the constructor, create the `QTimer` as a child object of `My_Class` (using `new QTimer(this)`). You can also move the `timer` initialisation code to the constructor and leave only `timer->start()` in `My_Class::process()` – RobbieE Feb 28 '14 at 13:38
  • @Dmitry: The official documentation (http://qt-project.org/doc/qt-5/qthread.html) says "From Qt 4.8 onwards, it is possible to deallocate objects that live in a thread that has just ended, by connecting the finished() signal to QObject::deleteLater()" – JKSH Mar 01 '14 at 03:49