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. QByteArray possible memory Leak ?
QtWS25 Last Chance

QByteArray possible memory Leak ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.6k 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    Hi ,

    Whane I trace the memory leak the Mac Instruments app point me below code :

    mq_dataLoad *dl = new mq_dataLoad;  //QVariant . includes image and text///
        QImage image = qpix.toImage();
    
        QByteArray ba_catch;
        
    
        QBuffer buffer_catch(&ba_catch);
    
        buffer_catch.open(QIODevice::WriteOnly);
        image.save(&buffer_catch, "PNG");
        dl->image = ba_catch;
        dl->text = MqttUserName + ":" + QString::number(mq_chip_code) ;
        mq_chip_code++;
        QByteArray byteArray_;
    
        QDataStream stream(&byteArray_ , QIODevice::WriteOnly);
        stream.setVersion(QDataStream::Qt_5_10);
    
    free(dl);
    

    Do you have any idea why this is leaking memory ?

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      There is no byte array anywhere. It's a false positive of your tool. Also you tool should tell exactly where the leak should be.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Just a note.
        free(dl);
        should that not be
        delete dl;
        since you are creating it with new ?

        1 Reply Last reply
        6
        • R RahibeMeryem

          Hi ,

          Whane I trace the memory leak the Mac Instruments app point me below code :

          mq_dataLoad *dl = new mq_dataLoad;  //QVariant . includes image and text///
              QImage image = qpix.toImage();
          
              QByteArray ba_catch;
              
          
              QBuffer buffer_catch(&ba_catch);
          
              buffer_catch.open(QIODevice::WriteOnly);
              image.save(&buffer_catch, "PNG");
              dl->image = ba_catch;
              dl->text = MqttUserName + ":" + QString::number(mq_chip_code) ;
              mq_chip_code++;
              QByteArray byteArray_;
          
              QDataStream stream(&byteArray_ , QIODevice::WriteOnly);
              stream.setVersion(QDataStream::Qt_5_10);
          
          free(dl);
          

          Do you have any idea why this is leaking memory ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @RahibeMeryem

          mq_dataLoad *dl = new mq_dataLoad; //QVariant . includes image and text///
          free(dl);

          As @mrjj has observed, this is wrong. free will not call QVariant's destructor, and at a guess that will be enough for a memory leak. Since you have previously gone dl->image = ba_catch;, that's a QByteArray, so it's probably where the leak is.

          1 Reply Last reply
          5
          • Christian EhrlicherC Christian Ehrlicher

            There is no byte array anywhere. It's a false positive of your tool. Also you tool should tell exactly where the leak should be.

            R Offline
            R Offline
            RahibeMeryem
            wrote on last edited by RahibeMeryem
            #5

            @Christian-Ehrlicher

            what is the suggested memory leak detection tool ? valgrind keep very slow by the way.

            @JonB its struct :

            struct mq_dataLoad {
                    QString text;
                    QByteArray image;
                };
            
            JonBJ 1 Reply Last reply
            0
            • R RahibeMeryem

              @Christian-Ehrlicher

              what is the suggested memory leak detection tool ? valgrind keep very slow by the way.

              @JonB its struct :

              struct mq_dataLoad {
                      QString text;
                      QByteArray image;
                  };
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @RahibeMeryem
              I don't see how it's such a struct given that the comment against it says QVariant, but there you are....

              Yes valgrind will be "slow". Before you worry about anything else, have you or have you not tried replacing your free(dl) with delete dl?

              1 Reply Last reply
              2
              • R Offline
                R Offline
                RahibeMeryem
                wrote on last edited by
                #7

                Lets try to delete and let you know...

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  RahibeMeryem
                  wrote on last edited by
                  #8

                  @JonB delete looks worked in Mac . It shows zero leak.. very good news.

                  What is interesting that same code looks leaking in ubuntu 18.04 ! How is that possible ?

                  Is there any tools like "Instruments app mac os x " in Linux OS ?

                  valgrind crashing when the thread started in app, still trying but ...

                  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