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. Correct release of memory (QListWidgetItem's)
QtWS25 Last Chance

Correct release of memory (QListWidgetItem's)

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

    Hello all,

    how do I correctly / properly release memory?

    The following example. I have a function that loads images into items and displays them:

    void ImportView::loadPhotos() {
    Q_FOREACH(QString photo, photos) {
            QApplication::processEvents();
            QListWidgetItem *item = new QListWidgetItem(photo, photoListWidget);
            QPixmap tempPhoto(dirpath + "/" + photo);
            item->setData(Qt::DecorationRole, tempPhoto.scaled(tempPhoto.width()/24, tempPhoto.height()/24, Qt::KeepAspectRatio));
            photoListWidget->addItem(item);
            loadProgressBar->setValue(loadProgressBar->value() + 1);
        }
    }
    

    Works also so far quite well, by scaling or displaying the thumbnails comes even with large lists (5k images) not really a strong memory usage.

    Regardless, when I click on a button now I add the paths to a file, but would of course free up the memory I used before (the items are not needed now after all). For example when I press the cancel button:

    void ImportWidget::onCancelButton_Clicked() {
        
    }
    

    Anybody have an idea/approach? I had thought I would load each item additionally into a QList QList<QListWidgetItem*> items, also run a foreach over it and then do a delete on each individual item. Would this make sense?

    How do I release such memory correctly in general? :)

    Thanks a lot!

    Merry Christmas!
    Jan

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There's no need for a separate list, you have already your QListWidget that provides all information. Do you want to clear all the items you created when calling cancel ? If so QListWidget has a clear method.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      jkx1J 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        There's no need for a separate list, you have already your QListWidget that provides all information. Do you want to clear all the items you created when calling cancel ? If so QListWidget has a clear method.

        jkx1J Offline
        jkx1J Offline
        jkx1
        wrote on last edited by jkx1
        #3

        @SGaist After loading all those images (I have it in a separate function) to the QListWidget, RAM usage gets a little bit high (which is ok).

        I want to delete the items completely and free up RAM (the items aren't used anymore, only for displaying before clicking on Import, Cancel etc.).

        mrjjM 1 Reply Last reply
        0
        • jkx1J jkx1

          @SGaist After loading all those images (I have it in a separate function) to the QListWidget, RAM usage gets a little bit high (which is ok).

          I want to delete the items completely and free up RAM (the items aren't used anymore, only for displaying before clicking on Import, Cancel etc.).

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @jkx1
          Hi
          Actually, your photoListWidget has ownership of the items you give it and will delete them when deleted itself.

          So if you delete the ImportWidget (i assume this work s like a window) after you used it, it will, in turn, delete the listwidget which in turn deletes
          its items.

          However, you can also just call clear() as SGaist shows as that will make it delete all items right there.

          jkx1J 1 Reply Last reply
          1
          • mrjjM mrjj

            @jkx1
            Hi
            Actually, your photoListWidget has ownership of the items you give it and will delete them when deleted itself.

            So if you delete the ImportWidget (i assume this work s like a window) after you used it, it will, in turn, delete the listwidget which in turn deletes
            its items.

            However, you can also just call clear() as SGaist shows as that will make it delete all items right there.

            jkx1J Offline
            jkx1J Offline
            jkx1
            wrote on last edited by
            #5

            @mrjj Hey! Does clear() also free up memory?

            When I click on Cancel button, a slot in MainWindow is triggered. The ImportWidget works like a widget, which is not displayed as a separate window. When I click on cancel, I tried this:

            void MainWindow::importPhotosView_onCancelButton_Clicked() {
                importPhotosView->importBar->hide();
                delete importPhotosView;
                headBar->show();
                importPhotosAction->setEnabled(true);
            
                setCentralWidget(0);
            }
            

            and this:

            void MainWindow::importPhotosView_onCancelButton_Clicked() {
                importPhotosView->importBar->hide();
                importPhotosView->close();
                headBar->show();
                importPhotosAction->setEnabled(true);
            
                setCentralWidget(0);
            }
            

            with WA_DeleteOnClose in ImportPhotosView.

            Neither of it works. I add around 50 CR2 raw images (extreme test), the memory hogs at around 1,4GB and clicking on cancel does not free up anything.

            Thats my problem.

            Many thanks!
            Jan

            JonBJ 1 Reply Last reply
            0
            • jkx1J jkx1

              @mrjj Hey! Does clear() also free up memory?

              When I click on Cancel button, a slot in MainWindow is triggered. The ImportWidget works like a widget, which is not displayed as a separate window. When I click on cancel, I tried this:

              void MainWindow::importPhotosView_onCancelButton_Clicked() {
                  importPhotosView->importBar->hide();
                  delete importPhotosView;
                  headBar->show();
                  importPhotosAction->setEnabled(true);
              
                  setCentralWidget(0);
              }
              

              and this:

              void MainWindow::importPhotosView_onCancelButton_Clicked() {
                  importPhotosView->importBar->hide();
                  importPhotosView->close();
                  headBar->show();
                  importPhotosAction->setEnabled(true);
              
                  setCentralWidget(0);
              }
              

              with WA_DeleteOnClose in ImportPhotosView.

              Neither of it works. I add around 50 CR2 raw images (extreme test), the memory hogs at around 1,4GB and clicking on cancel does not free up anything.

              Thats my problem.

              Many thanks!
              Jan

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @jkx1
              Why are you expecting anything to return memory used to the OS? All you can rely on is that after freeing it should re-use the memory, if/when it needs it. Does it do so? If you add one at a time and then free before adding the next one, how does memory go?

              1 Reply Last reply
              0
              • jkx1J Offline
                jkx1J Offline
                jkx1
                wrote on last edited by
                #7

                Got the problem. Has something to do with the QtRaw plugin. When loading insane amount of JPGs on close they get deleted. Raw images not...

                I investigate this...

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

                  Hi
                  yes deletes all items and what ever they contain including the
                  Qt::DecorationRole.

                  Its not surely you can see the mem returns to the OS at once. At least not with say
                  task manager.

                  This one ?
                  https://github.com/FMeinicke/QtRaw

                  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