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. Releasing QImage memory when going out of scope
Forum Updated to NodeBB v4.3 + New Features

Releasing QImage memory when going out of scope

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 17.6k 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #2

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jh224
      wrote on last edited by
      #3

      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 mgt.....my problem is with QImage mem mgt.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jh224
        wrote on last edited by
        #4

        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 @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stereomatching
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jh224
            wrote on last edited by
            #6

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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #7

              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?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stereomatching
                wrote on last edited by
                #8

                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

                .hpp
                @
                class QImageTest : public QObject
                {
                Q_OBJECT
                public:
                explicit QImageTest(QObject *parent = 0);

                signals:
                void send_image(QImage const &img);

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

                };
                @

                .cpp
                @
                QImageTest::QImageTest(QObject *parent) :
                QObject(parent)
                {
                }

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

                main
                @
                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;
                    label.setPixmap(QPixmap::fromImage(img));
                    label.show(); 
                    
                    a.exec&#40;&#41;;      
                }
                
                QImage img(ptr, size.width(&#41;, size.height(&#41;, format);    
                
                //will crash
                /*QLabel label;
                label.setPixmap(QPixmap::fromImage(img));
                label.show();*/
                

                @

                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

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    stereomatching
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #11

                      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. ;-)

                      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