Results 1 to 5 of 5

Thread: Proper way to delete a QMap

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Proper way to delete a QMap

    Hi, I have a QMap filled with pointers to objects, and I'd like to ask what's the proper way to delete them.

    I planned to loop though the map and delete the items one by one... but I saw the "clear()" method.

    void QMap::clear ()
    Removes all items from the map.


    Does it means "clear()" will delete dynamic objects and free the memory?

    thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Proper way to delete a QMap

    have a look at qDeleteAll.
    QMap::clear doesn't delete pointers.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Proper way to delete a QMap

    that's it.

    thanks!

  4. #4
    Join Date
    Feb 2020
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Proper way to delete a QMap

    Things which should be taken care when using QDeleteAll with QMap
    clear() will only clear the pointers, it will not delete the memory associated with it.
    QDeleteAll will work on the value part of the QMap.
    e.g QMap<QString, SomeClass*> mymap;
    so when you do QDeleteAll on 'mymap' it will delete value part.
    if you have thing like
    e.g QMap<SomeClass*, SomeClass*> extendedMap;
    as QDeleteAll only deletes value part and not the keys.
    you have to specifically delete keys as well.

    and then call clear() on it so that it will not have dangling pointers

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,476
    Thanks
    252
    Thanked 709 Times in 699 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Proper way to delete a QMap

    e.g QMap<SomeClass*, SomeClass*> extendedMap;

    as QDeleteAll only deletes value part and not the keys. you have to specifically delete keys as well and then call clear() on it so that it will not have dangling pointers
    BUT, if you are using this map to associate pointers to instances of the same class and it is a two-way map (i.e. the same pointer appears as both a key -and- a value), then you do not want to delete both the keys and the values, because then you will be double-deleting everything. Only if it is a one-way map where keys and values are pointers which appear only on one side do you want to delete both.

    IMO, the easiest (and safest) way to store pointers in a QMap (or std:: map, or really any collection data structure) and guarantee that the object instances they point to will be deleted when the map is cleared is to use smart pointers, for example QSharedPointer or std:: shared_ptr. By using shared pointers, you create the object, wrap it in a shared pointer, and forget about its lifetime. No memory leaks because you called clear() instead of deleteAll(), because with smart pointers clear() has the same effect as deleteAll(). And especially if you are storing pointers in both a collection and in some other data structure, using a smart pointer will guarantee that you won't accidentally delete a pointer and leave a dangling reference somewhere else.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. Best way for a graphicsitem to delete itself
    By pherthyl in forum Qt Programming
    Replies: 7
    Last Post: 22nd June 2008, 05:57
  3. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38
  4. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  5. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08

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.