Important: Please read the Qt Code of Conduct -

Releasing QImage memory when going out of scope

  • I have a class that create new QImage in the function of that class. When the object goes out scope, the memory from the QImage is not released. What is the proper way to use QImage without memory leaks? I believe this has been identified as a bug. What are the work arounds?

  • The QImage could handle the memory by itself if you declared it like this

    QImage img;

    if you "new" it
    //allocate memory and initialize the QImage
    //don't use malloc, because it wouldn't call constructor
    //atleast you have a good reason, don't do it
    QImage *img = new QImage;

    you have to delete it by yourself
    delete img; //free the memory

    you could use smart pointer to handle resource
    This is a better coding style than handling the
    resource by raw pointer

    //works like normal pointer but will be deleted after it is out of scope
    std::unique_ptr<QImage> img(new QImage);

    I think in most of the cases, first solution would be the best way

    Basis knowledge of memory management in C++ are not too difficult
    you could study "essential C++" if you want to grasp the idea quickly

  • When I use QImage img; it crashes.
    When I use new and then delete it, it crashes.

    I believe I'm ok with c++ mem problem is with QImage mem mgt.

  • I think part of my problem is passing the QImage in a signal. So maybe my question should be how do delete an QImage obj after emmiting it as a signal
    @ QImage img;
    emit mySignal(&img);
    /// this leaves the function unstable and it crashes on a 2nd func call

    QImage *img = new QImage;
    emit mySignal(img);
    delete img;
    /// this also leaves the function unstable and it crashes on a 2nd func call @

  • Could you reproduce your problems with minimize codes?
    I am developing some image processing software with Qt, openCV and boost::GIL
    Maybe your codes could help me avoid the trap too, thanks.

    what if you just pass the QImage like
    QImage img("lena.png");
    emit send(img);

  • Your code causes me a memory leak. It has been my experience with QT that memory releases when it goes out scope. However, this does not seems to be case with QImage. I guess the right answer is to create the class wide QImage and then load the image as needed and then delete it in the destructor. Do you know if there are any other work arounds? My code above is probably crashing because I'm deleting it before my signal is done with it....thus causing a null pointer situation.

    @ QImage img("lena.png");
    emit send(&img);

  • QImage is implicitly shared. You can create it on the stack or pass it by value without any copying happening or memory leaks occouring. If all shared instances have gone out of scope the memory is released.

    95% of all memory leaks are based upon the use of an improper tool to detect memory leaks.

    How do you identify the memory leak?

  • I beg of your pardon, but I don't know what is happening
    just seeing two lines of your codes
    I am not your compiler but a mere mortal

    class QImageTest : public QObject
    explicit QImageTest(QObject *parent = 0);

    void send_image(QImage const &img);

    public slots:
    void process_image(QImage const &img);


    QImageTest::QImageTest(QObject *parent) :

    void QImageTest::process_image(QImage const &img)
    qDebug() << "process image : "<< img.isNull();

    unsigned char *ptr = nullptr;
    QSize size;
    QImage::Format format;
    QImage img("lena2.jpg");
    size = img.size();
    format = img.format();
    ptr = img.bits();
    QImageTest test;
    test.process_image(img); //print false
    test.process_image(img); //print false

        QLabel label;
    QImage img(ptr, size.width(&#41;, size.height(&#41;, format);    
    //will crash
    /*QLabel label;


    Apparently, QImage already release the resource after going out of the scope

    atleast you pass it by value, and the process_image is working at another thread
    else QImage would collect the garbage automatically after going out of the scope

  • I'm not quite sure how this relates to your initial question, as the memory obviously is released when the last reference goes out of scope.

    Be aware that implicitly shared classes behave like all smart pointers do. You are safe as long as you use them, if you don't (line #8), you are on your own.

    This means that implicit sharing will happen if, and only if, you are using QImage to reference your object (<code>ptr</code> is not).

  • These codes show that the resource already
    release by QImage and It is hard to figure out
    what is happenning just looking at something like

    QImage img("lena.png");
    emit send(&img);

  • I, for whatever reason, thought you've raised the initial question, so I've wondered why you've shown such an example. But you are obviously not, and it starts making sense now. ;-)

Log in to reply