Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. deleting a QMap after clear()

deleting a QMap after clear()

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 541 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    nemd
    wrote on last edited by
    #1

    Hi,

    is it safe to call delete directly after having called clear() on a QMap.
    e.g. :

    QMap *myMap = new QMap<QString,MyClass>
    myMap->insert("one", myClass);
    ....
    myMap->clear();
    delete myMap;

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi and welcome to the forums
      yes it should be.
      The clear happens in place and will delete the internal structures.

      1 Reply Last reply
      4
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        If your value objects are pointer type, you should delete them as well.

        will delete the internal structures.
        @mrjj curious about above statement. What do you mean by it ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        mrjjM 1 Reply Last reply
        3
        • dheerendraD dheerendra

          If your value objects are pointer type, you should delete them as well.

          will delete the internal structures.
          @mrjj curious about above statement. What do you mean by it ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dheerendra
          I simply meant that calling clear deletes QMaps internal structures holding the items it manages.
          But good call noting if MyClass is a pointer , clear() will not be delete the object being pointed to, but only the pointer itself.

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            And to complete it all together - there is no need to create a QMap on the heap at all in most of the cases.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3

            • Login

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