简体   繁体   English

如何在QCoreApplication析构函数中删除对象和线程实例

[英]How to delete objects and thread instances in QCoreApplication destructor

  //my_class.h

  void My_class::My_Class(){

     my_ip=...
     my_port=...
  };

  void Data_Send_Func(){

 // send data over tcp;

 }

  void My_Class::~My_Class(){

   cout<<"Delete objects"<<endl;

  }

  void My_Class::process(){

     QTimer my_timer;
     my_timer->setInterval(30);
     connect(my_timer,SIGNAL(timeout()),this,SLOT(Data_Send_Func()));
     connect(this,SIGNAL(destroyed()),mytimer,SLOT(deleteLater));
  }


  //my_application.cpp
  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())) ;        

 }

  //my_application.h

 struct Exit_App{

    Exit_App(){

     signal(SIGINT,&Exit_App::Exit_F); 
     signal(SIGTERM,&Exit_App::Exit_F); 
     signal(SIGBREAK,&Exit_App::Exit_F);  

   }
     static void Exit_F(int sig){

       cout<<"Exiting App"<<endl;

       QCoreApplication::exit(0);
     }
 }


 int main(argc,char* argv[]){

     Exit_App app;
     My_application a(arcg,argv);
     return a.exec();

 } 

So when I close my program I want to delete the objects in My_Class destructor. 因此,当我关闭程序时,我想删除My_Class析构函数中的对象。

I dont want to emit fnished signal in application so I want to delete the my_class object and thread in My_application destructor. 我不想在应用程序中发出fnished信号,所以我想在My_application析构函数中删除my_class对象和线程。

   My_application::~My_application{
   if (my_class->thread()) {

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

     my_class->deleteLater();

  } else {

     delete my_class; // It's a threadless object, we can delete it
    thread->quit();
     }
    thread->wait();

 }

A QObject cannot be deleted from any thread but the one it's in, unless it is thread less (due to its thread having finished). QObject不能从任何线程中删除,而是从它所在的线程中删除,除非线程较少(由于其线程已完成)。 The following must hold: 必须具备以下条件:

Q_ASSERT(!object->thread() || object->thread() == QThread::currentThread());
delete object;

In case of an object that has a thread, you need to invoke the object's deleteLater method. 如果对象具有线程,则需要调用对象的deleteLater方法。 The deletion will be done within the thread's event loop. 删除将在线程的事件循环中完成。 When the object is deleted, it's time to finish the thread. 当对象被删除时,是时候完成线程了。 So: 所以:

if (my_class->thread()) {
  connect(my_class, SIGNAL(destroyed()), thread, SLOT(quit());
  my_class->deleteLater();
} else {
  delete my_class; // It's a threadless object, we can delete it
  thread->quit();
}
thread->wait();

Note that it's OK to call quit() and wait() on a thread that is finished. 请注意,可以在已完成的线程上调用quit()wait() It is redundant to check for a running thread like you do. 像你一样检查正在运行的线程是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM