Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QThread: Free heap memory?



  • I'm developing my application using QThread following this tutorial here.

    // --- PROCESS ---
    // Start processing data.
    void Plugin::process() {
        // allocate resources using new here
        double* B = new double[n];
    
        qDebug("Hello World!");
        emit finished();
    }
    

        QThread* thread = new QThread;
        worker->moveToThread(thread);
    
        connect(worker,  &Plugin_API::sendMsg, this, &job_manager::pluginMessage);
        connect(thread, &QThread::started, worker, &Plugin_API::start);
        connect(worker, &Plugin_API::finished, thread, &QThread::quit);
        connect(worker, &Plugin_API::finished, selectedPlugin, &Plugin_API::deleteLater);
        connect(thread, &QThread::finished, thread, &QThread::deleteLater);
    
        thread->start();
    

    My question is:
    Should I manually free the B variable memory? Or when the process() method ends, all allocated heap memory is cleared automatically?



  • @fem_dev said in QThread: Free heap memory?:

    all allocated heap memory is cleared automatically?

    Nah fam, this is not some nice walk in the park, this is C++!

    you can use std::unique_ptr<double[]> b(new double[n]); if you want memory to be wiped automaticaly (remember to #include <memory> for that)



  • @fem_dev said in QThread: Free heap memory?:

    all allocated heap memory is cleared automatically?

    Nah fam, this is not some nice walk in the park, this is C++!

    you can use std::unique_ptr<double[]> b(new double[n]); if you want memory to be wiped automaticaly (remember to #include <memory> for that)


Log in to reply