Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. This seems redundant to me? Delete each single value of a map and then

This seems redundant to me? Delete each single value of a map and then

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 5 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnFrom
    wrote on last edited by
    #1
    QMap<QString, QPixmap*>::const_iterator item;
       for( item = item_container.begin(); item !=  item_container.end(); item++){
           delete item.value();
       }
    item_container.clear();
    

    Doesn't clear() clears everything? Why does the code do it 'twice'?

    J.HilkJ 1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      QPixmap <pointer> - I'd say it's cleaning up allocated memory via a pointer.
      delete is gracefully destroying the object at the memory address is item.value() gives.
      clear is the elements / container cleanup.

      1 Reply Last reply
      3
      • J JohnFrom
        QMap<QString, QPixmap*>::const_iterator item;
           for( item = item_container.begin(); item !=  item_container.end(); item++){
               delete item.value();
           }
        item_container.clear();
        

        Doesn't clear() clears everything? Why does the code do it 'twice'?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @JohnFrom the code doesn't do it twice.

        You're maping QPixmaps that are created on the heap and therefore have to be deleted. Calling delete on the objects deletes it. But that leaves the QMap still intact, with pointers to memory, that is no longer valid.

        clear only removes references / pointers from the QMap.

        If you only call clear, the QMap is reset, but the memory is still occupied -> memory leak.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        6
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          2 side notes:

          • if the QMap owns those QPixmap then there's (close to) no advantage in storing it as QMap<QString, QPixmap*> (Qt internally optimises the processes so that it's only marginally worse than a raw pointer) only the burden of deleting the values manually.
            Consider changing it to QMap<QString, QPixmap> if appropriate
          • do not mix const and non-const iterators. Either declare QMap<QString, QPixmap*>::iterator item; or use item_container.cbegin()/item_container.cend()

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Or use qDeleteAll and then clear your container.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved