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. Deleting pointers in slot
Qt 6.11 is out! See what's new in the release blog

Deleting pointers in slot

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 474 Views
  • 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by pingal
    #1

    This is a member function which I use for uploading files using HTTP

    void HTTP::UploadFile(QNetworkRequest request, QFileInfo filePath){
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        multiPart_list.append(multiPart); // Store in a list
        .
        .
        .
        UploadManager.post(request, multiPart);
    }
    

    When uploading is complete, below slot is called by QNetworkAccessManager::finished signal

    void HTTP::UploadingFinished(QNetworkReply *reply){
        qDebug() << "Uploading Finished!";
        reply->deleteLater();
    }
    

    I want to delete multiPart* in HTTP::UploadingFinished() slot (to avoid memory leak).
    Everytime HTTP::UploadFile(..) is called, A new multiPart is allocated and appended to the below QList

    QList<QHttpMultiPart*> multiPart_list; // Class Attribute 
    

    I need a mechanism clean these pointer in UploadingFinished(..) slot.

    jsulmJ 1 Reply Last reply
    0
    • P pingal

      This is a member function which I use for uploading files using HTTP

      void HTTP::UploadFile(QNetworkRequest request, QFileInfo filePath){
      
          QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
          multiPart_list.append(multiPart); // Store in a list
          .
          .
          .
          UploadManager.post(request, multiPart);
      }
      

      When uploading is complete, below slot is called by QNetworkAccessManager::finished signal

      void HTTP::UploadingFinished(QNetworkReply *reply){
          qDebug() << "Uploading Finished!";
          reply->deleteLater();
      }
      

      I want to delete multiPart* in HTTP::UploadingFinished() slot (to avoid memory leak).
      Everytime HTTP::UploadFile(..) is called, A new multiPart is allocated and appended to the below QList

      QList<QHttpMultiPart*> multiPart_list; // Class Attribute 
      

      I need a mechanism clean these pointer in UploadingFinished(..) slot.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @pingal If you take a look at https://doc.qt.io/qt-5/qhttpmultipart.html you will see:

      multiPart->setParent(reply); // delete the multiPart with the reply
      

      Why not doing it this way instead of implementing complex logic with lists?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P Offline
        P Offline
        pingal
        wrote on last edited by
        #3

        Thanks, Actually i'm not used to QObject->setParent(..). But that solved my problem

        jsulmJ 1 Reply Last reply
        0
        • P pingal

          Thanks, Actually i'm not used to QObject->setParent(..). But that solved my problem

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @pingal said in Deleting pointers in slot:

          Actually i'm not used to QObject->setParent(..)

          Take a look at https://doc.qt.io/qt-5/objecttrees.html - this is an important concept in Qt.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pingal
            wrote on last edited by
            #5

            That's helpful. I think this is the problem for people like me who come to QT when they are used to plain C++ :p

            1 Reply Last reply
            0

            • Login

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