Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread: Free heap memory?
Forum Updated to NodeBB v4.3 + New Features

QThread: Free heap memory?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 394 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    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?

    VRoninV 1 Reply Last reply
    0
    • F fem_dev

      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?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @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)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved