Results 1 to 5 of 5

Thread: When I use Listview with icons its causing memory leak

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Question When I use Listview with icons its causing memory leak

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: When I use Listview with icons its causing memory leak

    Where do you clear (and delete the items) of a list in AppstoreItems in iAppStoreChannel?:
    Qt Code:
    1. QList<AppstoreItem*> items = iAppStoreChannel->getItems();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: When I use Listview with icons its causing memory leak

    Hi, these belong to the AppSotreChannel Class, it owns this data.


    Guus

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: When I use Listview with icons its causing memory leak

    Never the less it must also take cere of deleting it, you have to make sure everywhere in your application that any dynamically allocated memory is also released!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: When I use Listview with icons its causing memory leak

    You can use Insure++ to assist you here:

    http://www.parasoft.com/jsp/resource...rror_detection

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 13:28
  3. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  4. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  5. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.