Search code examples
c++multithreadingqtqthread

Why is QThread deleteLater still required if I am not using a pointer object of QThread?


class Controller : public QObject
{
    Q_OBJECT

private:
    Two objTwo;

    QThread objQThread;

    Controller();

public slots:
    void mySlot(){}
};

Controller::Controller()
{
    objTwo.moveToThread( &objQThread );

    connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
    connect( &objQThread, &QThread::finished, &objQThread, &QThread::deleteLater );

    objQThread.start();
}

Here the QThread's object is not a pointer, so will it still be required to use deleteLater here? Is it appropriate to use class objects there instead of pointers?

I thought deletion can be prevented that way.


Solution

  • Here the QThread's object is not a pointer, so will it still be required to use deleteLater here?

    No.

    Is it appropriate to use class objects there instead of pointers?

    Totally, don't use pointers if you don't need to.

    I thought deletion can be prevented that way.

    Manual deletion can be prevented this way, yes.