[Interest] How to get destructor called in object that was moved to a thread?

Allan Sandfeld Jensen kde at carewolf.com
Mon Aug 17 23:44:05 CEST 2015


On Monday 17 August 2015, Murphy, Sean wrote:
> I'm struggling to get my destructor called in a class of mine that is moved
> to a separate thread
> 
> In my class's header file:
> class myClass
> {
>   Q_OBJECT
>   ...
> }
> 
> In my class's .cpp file:
> myClass::~myClass()
> {
>   qDebug("myClass destructor");
> }
> 
> MainWindow.h:
>   QThread* myThread;
>   myClass* mine;
> 
> My mainwindow's .cpp file:
> MainWindow::MainWindow(QWidget *parent)
> {
>   mine = new myClass();
>   myThread = new QThread(this);
>   mine->moveToThread(myThread);
>   myThread->start();
> }
> 
>   MainWindow::~MainWindow()
> {
>     myThread ->exit();
>     delete ui;
> }
> 
> When I run my application, myClass functions properly, signals and slots in
> it are being called. But when the user clicks the MainWindow's 'X' button
> to close the application, the destructor for my class is never called, or
> at least that qDebug() statement is never printed to the console.
> 
> Any thoughts?

Where do you delete mine? I see no delete nor any ownership. Note that unless 
you move it back you need to delete it from the body of the new thread. If the 
new thread has a runloop you can also async call the slot mine->deleteLayer().

`Alla



More information about the Interest mailing list