I am using QT 4.6
Nokia N8 (Symbian)

I am having a bit of a memory issue using the SlidingStackedWidget to hold nine list view widgets, that each display approx 20 items.

To try and conserve memory I am loading only the images for the currently displayed widget.
I am controlling the loading/unloading by capturing the widget event for show and hide, and using this to repolulate my model for the list view.

I am also using the PixmapCache as a mechanism to cache images that I have loaded, I have set my cache size to 2048k

My code for loading the pixmap, and populating / clearing the list model is listed below.

When I run this on the Nokia N8, and transition between widgets, I very quickly run out of memory, when loading images.

From my observations (I have included the code I use to trace memory usage) there is a mismatch between the amount of data allocated between
the populateModel and clearModel methods. When I transition to a new screen the memory allways increases, and when I clear the model, it never seems to remove enough memory.

1. I am concerned that I am misusing the PixmapCache, the help states,
"QPixmaps are automatically added to the QPixmapCache when loaded from a file; the key used is internal and can not be acquired."
Should I be bothering to cache Pixmaps as I load them, as it would appear the system is already doing this for me?

2. Does the models appendRow method take ownership of the item data?
do I need to clear the model row by row, prior to calling clear?

3. can anyone point out where I am leaking memory?

thanks in advance

Guus Davidson

Qt Code:
  1. #ifdef Q_OS_SYMBIAN
  2. TInt mem, size, limit;
  3. User::Heap().AllocSize(mem);
  4. size = User::Heap().Size();
  5. limit = User::Heap().MaxLength();
  6.  
  7. qDebug() << QString("Heap Mem : %1").arg(mem);
  8. qDebug() << QString("Heap Size : %1").arg(size);
  9. qDebug() << QString("Heap Limit : %1").arg(limit);
  10. #endif
To copy to clipboard, switch view to plain text mode 



Qt Code:
  1. QPixmap FileImageCache::getPixmap(const QString& key)
  2. {
  3. QPixmap pm;
  4. if (!QPixmapCache::find(key, &pm))
  5. {
  6. pm.load(diskCacheDirectory + "//" +key);
  7. bool result = QPixmapCache::insert(key, pm);
  8. return pm;
  9. }
To copy to clipboard, switch view to plain text mode 



Qt Code:
  1. void DisplayWidget::populateModel()
  2. {
  3. if(iModelCleared == true)
  4. {
  5. BaseDisplayWidget::populateModel();
  6.  
  7. QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(iListView->model());
  8. int defaultIconCount = 0;
  9. if(model != 0 && iAppStoreChannel != 0)
  10. {
  11. QList<AppstoreItem*> items = iAppStoreChannel->getItems();
  12.  
  13. int count = items.count();
  14. AppstoreItem* currentItem = 0;
  15.  
  16. for(int i = 0; i < count ; i++)
  17. {
  18. bool defaultIconUsed = true;
  19. currentItem = items.at(i);
  20.  
  21. QString url = currentItem->getIconUrl();
  22. QIcon icon_image;
  23. if(url.length() > 0)
  24. {
  25. QString key = iImageCache->getKeyForImageLocation(url);
  26. if(iImageCache->containsKey(key) == true)
  27. {
  28. QPixmap pixmap= iImageCache->getPixmap(key);
  29.  
  30. if(!pixmap.isNull())
  31. {
  32. defaultIconUsed = false;
  33. icon_image.addPixmap(pixmap);
  34. }
  35. else
  36. {
  37. defaultIconCount ++;
  38. }
  39. }
  40. }
  41.  
  42. isdefaultIcon.append(defaultIconUsed);
  43.  
  44. item->setData(currentItem ->getHeadline(),ChannelListViewDelegate::headerTextRole);
  45. item->setData(currentItem ->getDescription(),ChannelListViewDelegate::subHeaderTextrole);
  46. if(icon_image.isNull())
  47. {
  48. item->setData(*icon_default,ChannelListViewDelegate::IconRole);
  49. }
  50. else
  51. {
  52. item->setData(icon_image,ChannelListViewDelegate::IconRole);
  53. }
  54. model->appendRow(item);
  55. }
  56. }
  57. }
  58. }
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. void DisplayWidget::clearModel()
  2. {
  3. if(iModelCleared == false)
  4. {
  5. QStandardItemModel* ptr = dynamic_cast<QStandardItemModel*>(iListView->model());
  6. if(ptr != 0)
  7. {
  8. ptr->clear();
  9. iModelCleared = true;
  10. delete ptr;
  11. }
  12. }
  13. }
To copy to clipboard, switch view to plain text mode