QFuture problem

  • Hello!

    I am using a QFuture to call a function that obtain the size of a directory. This function spend a lot of time and is called when I open a window so, in order to not blocking the window, I want to call it with a QFuture.

    The problem is, when I close the window before the QFuture is finished and I try to open the window again, I obtain a memory access error because I think the QFuture finish without the window. It tries to put the value on the label and this is the error.


    CleanWindow::CleanWindow(CConfig *pConfig, QWidget *parent) :
        ui(new Ui::CleanWindow)
        setWindowTitle("Clean window");
        setMinimumSize(1000, 650);
        setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
        QFuture<void> future = QtConcurrent::run([=]() {
        delete ui;
    void CleanWindow::obtainSize() {

    How can I delete or disable the QFuture when I close the window?

    Thank you very much!

  • Moderators

    hi @ivanicy

    the short answer is, you can't

    The longer explanation:
    QFuture is only meant to query the QtConcurrent thread, of its state, running/finished etc


    Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.

    You can however, set an atomic bool, that is check inside run and exit that way.

    Also your QFuture is local, and gets destroyed as soon as the constructor finished, regardless of the QtConcurrent running or not.

  • @ivanicy

    You could use the https://doc.qt.io/qt-5/qfuturesynchronizer.html and add the future. When the QFutureSynchronizer is destroyed, it will be wait until all futures are finished.

Log in to reply