1
votes

I have and application with a worker thread. When I try to stop the thread and close my application it crashes.

I am creating the worker and the thread and connecting the signals and slots as:

QPointer<QThread> monitorThread(new QThread());
QPointer<ItemMonitor> monitor(new ItemMonitor(items));
monitor->moveToThread(monitorThread);

//closeInitiated signal emitted when custom close button is clicked
connect(this, SIGNAL(closeInitiated()), monitor, SLOT(finishUp()));
connect(monitor, SIGNAL(finished()), monitorThread, SLOT(quit()));
connect(monitor, SIGNAL(finished()), monitor, SLOT(deleteLater()));
connect(monitor, SIGNAL(finished()), this, SLOT(closeApplication()));
connect(monitorThread, SIGNAL(started()), monitor, SLOT(beginMonitoring()));
connect(monitorThread, SIGNAL(finished()), monitorThread, SLOT(deleteLater()));

//start monitoring
monitorThread->start();

The ItemMonitor class which is running in the QThread is

#include "itemmonitor.h"
#include "todoitem.h"
#include <QObject>
#include <iostream>
#include <QTimer>


ItemMonitor::ItemMonitor(std::vector< QPointer<ToDoItem> >& items_)
:items(items_),
 shouldRun(true){
    std::cout << "Monitor created" << std::endl;
}


void ItemMonitor::beginMonitoring(){
    if(shouldRun){
        for(int i=0; i<items.size(); i++){
            items[i]->setSecsTillDeadline();
        }
        QTimer::singleShot(100, this, SLOT(beginMonitoring()));
    }else{
        emit finished();
    }
}

void ItemMonitor::finishUp(){
    shouldRun = false;
}

and the closeApplication function in my main class is:

void ToDoList::closeApplication(){
    while(monitorThread->isRunning()){
        std:: cout << "thread not closed" << std::endl;
    }
    QApplication::quit();
}

what is odd to me is that if I make no attempt to quit the thread and just try to close the applicationt then there are no errors

1
It's possible your call here "connect(monitor, SIGNAL(finished()), this, SLOT(closeApplication()));" could cause an issue if you're deleting while cleaning up the application. Are you attempting to close the application twice? I don't see where you try to stop the thread in your close function. - Chase Henslee
'Crashes' is meaningless. At least, you should look at the stack trace. - SergeyA
when I try to debug the application in qt debugger it never runs and freezes out my whole computer...i have to restart, so I can not see the stack trace - user2145312
im not trying to close the thread in the close function. the threads quit slot is being called from the monitors finished() signal - user2145312

1 Answers

2
votes
connect(monitor, SIGNAL(finished()), monitorThread, SLOT(quit()));
connect(monitorThread, SIGNAL(finished()), monitorThread, SLOT(deleteLater()));

monitor emit finished() -> monitorThread->quit() -> monitorThread emit finished() -> monitorThread->deleteLater() after the monitors emit finished() signal, the monitorThread will be deleted.

void ToDoList::closeApplication(){
    while(monitorThread->isRunning()){//maybe you should check the monitorThread for null
        std:: cout << "thread not closed" << std::endl;
    }
    QApplication::quit();
}