Results 1 to 5 of 5

Thread: memory leaks and deleting a QMap

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default memory leaks and deleting a QMap

    If we have a QMap<int, Something*>
    does deleting the QMap properly clean up the Something*?
    "Something" is not part of the object tree.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: memory leaks and deleting a QMap

    Yes, destroying the QMap "properly" cleans up the Someting pointers it owns. That has nothing to with the Something instances those pointers point at, which continue to exist because the container does not own them.

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

    ravas (13th November 2015)

  4. #3
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: memory leaks and deleting a QMap

    What is the appropriate loop to deal with the instances?
    example:

    Qt Code:
    1. foreach (Something* thing, a_map)
    2. {
    3. delete thing;
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: memory leaks and deleting a QMap

    More like this:

    Qt Code:
    1. QList< Something * > things = a_map.values();
    2. foreach( Something * thing, things )
    3. {
    4. delete thing;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Edit: although upon reading the docs, it looks like your code would accomplish the same thing. I am more familiar with std:: map<>, where the iterator returns an std:: pair<> containing the key and value.
    Last edited by d_stranz; 13th November 2015 at 22:13.

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

    ravas (14th November 2015)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: memory leaks and deleting a QMap

    Quote Originally Posted by d_stranz View Post
    Edit: although upon reading the docs, it looks like your code would accomplish the same thing.
    Definitely iterating over the map.
    Iterating over the values requires two iterations: one for creating the list of values and the the actual loop.

    In any case: http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll-1

    Cheers,
    _

  8. The following 2 users say thank you to anda_skoa for this useful post:

    d_stranz (16th November 2015), ravas (15th November 2015)

Similar Threads

  1. Memory leaks!
    By karlkar in forum Newbie
    Replies: 1
    Last Post: 28th June 2013, 22:37
  2. Memory Leaks
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2008, 16:26
  3. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 19:28
  4. creating/deleting Objects // memory leaks
    By janus in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 18:17
  5. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00

Tags for this Thread

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.