QByteArray possible memory Leak ?



  • 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 ?


  • Qt Champions 2018

    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.


  • Lifetime Qt Champion

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



  • @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.



  • @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;
        };
    


  • @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?



  • Lets try to delete and let you know...



  • @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 ...


Log in to reply