Home > Mobile >  Qt promoter thread method will not lead to a memory leak? The resources will be released?
Qt promoter thread method will not lead to a memory leak? The resources will be released?

Time:02-25

Qt official assistant for example
 class Worker: public QObject 
{
Q_OBJECT

Public slots:
Void doWork (const QString & amp; The parameter) {
QString result;
/*... Here is the expensive or blocking operation... */
Emit resultReady (result);
}

Signals:
Void resultReady (const QString & amp; The result);
};

The class Controller: public QObject
{
Q_OBJECT
QThread workerThread;
Public:
The Controller () {
Worker Worker *=new Worker;
The worker - & gt; MoveToThread (& amp; WorkerThread);

The connect (& amp; WorkerThread, & amp; QThread: : finished, the worker, & amp; QObject: : deleteLater);
The connect (this, & amp; Controller: : operate, worker, & amp; Worker: : doWork);
The connect (worker, & amp; Worker: : resultReady, this, & amp; Controller: : handleResults);
WorkerThread. Start ();
}
~ Controller () {
workerThread. Quit ();
WorkerThread. Wait ();

}
Public slots:
Void handleResults (const QString & amp;);
Signals:
Void operate (const QString & amp;);
};


Want to ask next, Worker * to move in after the child thread, can the Worker in the destructor * pointer resource normal release?

CodePudding user response:

Under normal circumstances will, because the destructor will you thread runs out, such as, according to you connect in the constructor, after the completion of the worker will call deleteLater ()

CodePudding user response:

Will.
You can add a destructor in the work, have a look at, to perform here,
  •  Tags:  
  • Qt
  • Related