Correct release of memory (QListWidgetItem's)
-
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 -
@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.).
-
@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.
-
@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 -
@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? -
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