Results 1 to 12 of 12

Thread: QPixmaps don't seem to deallocate from memory(solved)

  1. #1
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking QPixmaps don't seem to deallocate from memory(solved)

    Solved :
    Apparently the problem wasn't in deallocating images, it was in the command I used to get the image location(the database->cell method) I didn't consider this because the dataCache also used the method. But here is what was going on, the "thumbnail" + thumbnailSize column was set as type "Icon" which meant the database would link to the icon database and from that the current icon theme database. When it linked it would call the index->set method to update the index to the correct location:
    Qt Code:
    1. //Constructer
    2. Index::Index(const QString &location, QObject *parent)
    3. : AbstractIndex(location, parent)
    4. {
    5. set(location);
    6. }
    7.  
    8. //Destructer
    9. Index::~Index()
    10. {/* Do nothing*/}
    11.  
    12. /*
    13.  *sets the index's location
    14.  */
    15. void Index::set(const QString &location)
    16. {
    17. index = new Storage(location + "/" + "index", this);
    18. indexAll = new Storage(location + "/" + "index.all", this);
    19. indexEntrys = new Storage(location + "/" + "index.entrys", this);
    20. indexStartsWith = new Storage(location + "/" + "index.startswith", this);
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    so every time the set command was called all these storage objects would just be recreated without the old ones being deleted! I don't know how I made such a big mistake .

    Thanks everyone for your help .
    ~Timothy

    Hi,
    I have a method that fills a QList<QPixmap*> after attempting to delete its previous content:

    Qt Code:
    1. QPixmapCache::clear();
    2. thumbnails.clear();
    3. for(int row = 0; row < currentRows.size(); row++)
    4. {
    5. QPixmap image(database->cell(currentRows.at(row), "thumbnail" + thumbnailSize));
    6. thumbnails += image;
    7. }
    To copy to clipboard, switch view to plain text mode 

    However when I look at the application on KSystemGaurd its ram usage raises by 1 meg everytime that method is called (I can get my application to use 2Gb of ram just by calling that method enough times) If I remove the following lines:
    Qt Code:
    1. QPixmap *image = new QPixmap(database->cell(currentRows.at(row), "thumbnail" + thumbnailSize));
    2. thumbnails += image;
    To copy to clipboard, switch view to plain text mode 
    the memory leak dissappears, what am I doing wrong?

    Edit:
    here is the entire method:
    Qt Code:
    1. void DatabaseModel::display()
    2. {
    3. if(currentRows.size() > 0){
    4. beginRemoveRows(QModelIndex(), 0, currentRows.size());
    5. currentRows.clear();
    6. endRemoveRows();
    7. }
    8.  
    9. beginInsertRows(QModelIndex(), 0, currentRows.size());
    10. qint64 currentOffset = 0;
    11. currentRows = database->rows(positionCache.last(), maxShown, currentOffset);
    12. qDeleteAll(dataCache);
    13. dataCache.clear();
    14. QPixmapCache::clear();
    15. thumbnails.clear();
    16. for(int row = 0; row < currentRows.size(); row++)
    17. {
    18. QStringList *rowData = new QStringList;
    19. for(int col = 0; col < cols.size(); col++){
    20. rowData->append(database->cell(currentRows.at(row), cols.at(col)));
    21. }
    22. dataCache += rowData;
    23. QPixmap image(database->cell(currentRows.at(row), "thumbnail" + thumbnailSize));
    24. thumbnails += image;
    25. }
    26.  
    27. positionCache += currentOffset;
    28. endInsertRows();
    29. }
    To copy to clipboard, switch view to plain text mode 

    I am using the latest Qt update for kubuntu: 4.3.0-4.

    Thanks in advance ,
    ~Timothy
    Last edited by timothy.crosley; 14th August 2007 at 19:45.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmaps don't seem to deallocate from memory

    Try a:
    QPixmapCache::clear();
    before qDeleteAll();

  3. The following user says thank you to marcel for this useful post:

    timothy.crosley (14th August 2007)

  4. #3
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPixmaps don't seem to deallocate from memory

    I really appreciate the quick response; sadly though, this didn't seem to change anything . I updated the question to reflect this.
    ~Timothy

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPixmaps don't seem to deallocate from memory

    QPixmap is an implicitly shared class so I'd suggest allocating pixmaps on stack for easier memory management.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    timothy.crosley (14th August 2007)

  7. #5
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QPixmaps don't seem to deallocate from memory

    How do I allocate the pixmaps on stack?
    Thanks,
    ~Timothy

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPixmaps don't seem to deallocate from memory

    Without keyword "new":
    Qt Code:
    1. QList<QPixmap> thumbnails;
    2. ...
    3. // qDeleteAll(thumbnails); // remove this
    4. thumbnails.clear();
    5. ...
    6. QPixmap image = QPixmap(database->cell(currentRows.at(row), "thumbnail" + thumbnailSize));
    7. thumbnails += image;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    timothy.crosley (14th August 2007)

  10. #7
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPixmaps don't seem to deallocate from memory

    I should have known what you meant . Sadly this still doesn't fix the memory leak(I actually had it this way originally). I have been trying to fix this bug for 3 days now and its driving me insane .
    ~Timothy

  11. #8
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmaps don't seem to deallocate from memory

    Since you mentioned the QPixmapCache, what does QPixmapCache::cacheLimit () return.

  12. The following user says thank you to spud for this useful post:

    timothy.crosley (14th August 2007)

  13. #9
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPixmaps don't seem to deallocate from memory

    It returns 1024.

  14. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmaps don't seem to deallocate from memory

    If you have the possibility, step in the QPixmap destructor and see if it deletes the data member.
    Qt Code:
    1. [B]void QPixmap::deref()
    2. {
    3. if(data && data->deref()) { // Destroy image if last ref
    4. if (qt_pixmap_cleanup_hook_64)
    5. qt_pixmap_cleanup_hook_64(cacheKey());
    6. delete data;
    7. data = 0;
    8. }
    9. }[/B]
    To copy to clipboard, switch view to plain text mode 


    Regards

  15. The following user says thank you to marcel for this useful post:

    timothy.crosley (14th August 2007)

  16. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPixmaps don't seem to deallocate from memory(solved)

    What was the problem and how was it solved in the end?

    Edit: Oh, sorry. I didn't notice the first post being modified..
    Last edited by jpn; 15th August 2007 at 22:05.
    J-P Nurmi

  17. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmaps don't seem to deallocate from memory(solved)

    Quote Originally Posted by jpn View Post
    What was the problem and how was it solved in the end?
    See the first post .

Similar Threads

  1. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

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.