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. QMap iterator Bug??

QMap iterator Bug??

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 2.6k 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.
  • C Christian Ehrlicher
    1 Dec 2021, 06:13

    C++ stuff

    m_pLogin->getAccountMgr().begin()
    m_pLogin->getAccountMgr().end()

    You're creating two temporary container here which are out-of-scope so your program will crash.

    I Offline
    I Offline
    IknowQT
    wrote on 1 Dec 2021, 06:15 last edited by
    #3

    @Christian-Ehrlicher

    I don't quite understand, but usually iterate isn't used the way I wrote it? Should ++ increase from begin?

    C 1 Reply Last reply 1 Dec 2021, 06:26
    0
    • I IknowQT
      1 Dec 2021, 06:15

      @Christian-Ehrlicher

      I don't quite understand, but usually iterate isn't used the way I wrote it? Should ++ increase from begin?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Dec 2021, 06:26 last edited by
      #4

      @IknowQT said in QMap iterator Bug??:

      Should ++ increase from begin?

      Don't know what this has to do with my comment above.

      m_pLogin->getAccountMgr().begin()

      This creates a temporary container and calls begin on it, then the container is destroyed and the iterator is dangling.
      Create a local copy of the container you want to iterate over.

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

      I 1 Reply Last reply 1 Dec 2021, 06:35
      1
      • C Christian Ehrlicher
        1 Dec 2021, 06:26

        @IknowQT said in QMap iterator Bug??:

        Should ++ increase from begin?

        Don't know what this has to do with my comment above.

        m_pLogin->getAccountMgr().begin()

        This creates a temporary container and calls begin on it, then the container is destroyed and the iterator is dangling.
        Create a local copy of the container you want to iterate over.

        I Offline
        I Offline
        IknowQT
        wrote on 1 Dec 2021, 06:35 last edited by
        #5

        @Christian-Ehrlicher

        QMap<QString, CLogin::AccountInfo>::iterator iter = m_pLogin->getAccountMgr().begin();
        			for (iter; iter != m_pLogin->getAccountMgr().end(); iter++)
        			{
        				UpdateTableWidget(iter);
        			}
        

        Does it mean I should do it like this?

        C 1 Reply Last reply 1 Dec 2021, 06:39
        0
        • I IknowQT
          1 Dec 2021, 06:35

          @Christian-Ehrlicher

          QMap<QString, CLogin::AccountInfo>::iterator iter = m_pLogin->getAccountMgr().begin();
          			for (iter; iter != m_pLogin->getAccountMgr().end(); iter++)
          			{
          				UpdateTableWidget(iter);
          			}
          

          Does it mean I should do it like this?

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Dec 2021, 06:39 last edited by Christian Ehrlicher 12 Jan 2021, 06:40
          #6

          @IknowQT said in QMap iterator Bug??:

          Does it mean I should do it like this?

          No, since you still create two temporary container

          m_pLogin->getAccountMgr()...

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

          I 1 Reply Last reply 1 Dec 2021, 06:41
          0
          • C Christian Ehrlicher
            1 Dec 2021, 06:39

            @IknowQT said in QMap iterator Bug??:

            Does it mean I should do it like this?

            No, since you still create two temporary container

            m_pLogin->getAccountMgr()...

            I Offline
            I Offline
            IknowQT
            wrote on 1 Dec 2021, 06:41 last edited by
            #7

            @Christian-Ehrlicher

            Can you tell me in detail what to do?
            Can you tell me if you have an example code?

            C 1 Reply Last reply 1 Dec 2021, 06:41
            0
            • I IknowQT
              1 Dec 2021, 06:41

              @Christian-Ehrlicher

              Can you tell me in detail what to do?
              Can you tell me if you have an example code?

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 1 Dec 2021, 06:41 last edited by Christian Ehrlicher 12 Jan 2021, 06:43
              #8

              @IknowQT said in QMap iterator Bug??:

              Can you tell me in detail what to do?

              I already did:

              Create a local copy of the container you want to iterate over.

              Can you tell me if you have an example code?

              Maybe a c++ book would be a good start

              /edit: it's even described in the Qt documentation: https://doc.qt.io/qt-5/containers.html (Search for '// RIGHT' and '// WRONG')

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

              I 1 Reply Last reply 1 Dec 2021, 06:46
              0
              • C Christian Ehrlicher
                1 Dec 2021, 06:41

                @IknowQT said in QMap iterator Bug??:

                Can you tell me in detail what to do?

                I already did:

                Create a local copy of the container you want to iterate over.

                Can you tell me if you have an example code?

                Maybe a c++ book would be a good start

                /edit: it's even described in the Qt documentation: https://doc.qt.io/qt-5/containers.html (Search for '// RIGHT' and '// WRONG')

                I Offline
                I Offline
                IknowQT
                wrote on 1 Dec 2021, 06:46 last edited by
                #9

                @Christian-Ehrlicher

                QMap<QString, CLogin::AccountInfo> test = m_pLogin->getAccountMgr();
                			QMap<QString, CLogin::AccountInfo>::iterator iter;// = test.begin();
                			for (iter = test.begin(); iter != test.end(); ++iter)
                			{
                				UpdateTableWidget(iter);
                			}
                

                Are you telling me to write it like this?
                I tested it and it worked well.

                K 1 Reply Last reply 1 Dec 2021, 07:14
                0
                • I IknowQT
                  1 Dec 2021, 06:46

                  @Christian-Ehrlicher

                  QMap<QString, CLogin::AccountInfo> test = m_pLogin->getAccountMgr();
                  			QMap<QString, CLogin::AccountInfo>::iterator iter;// = test.begin();
                  			for (iter = test.begin(); iter != test.end(); ++iter)
                  			{
                  				UpdateTableWidget(iter);
                  			}
                  

                  Are you telling me to write it like this?
                  I tested it and it worked well.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 1 Dec 2021, 07:14 last edited by
                  #10

                  @IknowQT said in QMap iterator Bug??:

                  Are you telling me to write it like this?

                  What @Christian-Ehrlicher is telling you, is that you are creating 2 different container.
                  Each time you are calling m_pLogin->getAccountMgr(), it will return a QMap instance.
                  So each one has a different begin and end.
                  So you are comparing things which cannot be compared.

                  But yes, this is the right way to do, as it is given in Qt documentation:

                  // store local copy of the map
                  auto &test = m_pLogin->getAccountMgr();
                  // iterate through the map
                  for (auto iter = test.begin(); iter != test.end(); ++iter)
                  {
                      UpdateTableWidget(iter);
                  }
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 1 Dec 2021, 07:19 last edited by
                    #11

                    @KroMignon said in QMap iterator Bug??:

                    So each one has a different begin and end.

                    No, it's even worse - as I said above the two iterators are dangling because the returned containers are temporaries.

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

                    I 1 Reply Last reply 1 Dec 2021, 08:14
                    0
                    • C Christian Ehrlicher
                      1 Dec 2021, 07:19

                      @KroMignon said in QMap iterator Bug??:

                      So each one has a different begin and end.

                      No, it's even worse - as I said above the two iterators are dangling because the returned containers are temporaries.

                      I Offline
                      I Offline
                      IknowQT
                      wrote on 1 Dec 2021, 08:14 last edited by
                      #12

                      @Christian-Ehrlicher

                      I think this topic is endless. Can you post some concrete example code?

                      J K 2 Replies Last reply 1 Dec 2021, 08:16
                      0
                      • I IknowQT
                        1 Dec 2021, 08:14

                        @Christian-Ehrlicher

                        I think this topic is endless. Can you post some concrete example code?

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 1 Dec 2021, 08:16 last edited by
                        #13

                        @IknowQT said in QMap iterator Bug??:

                        I think this topic is endless. Can you post some concrete example code?

                        You already have it and you even wrote that it is working...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        4
                        • I IknowQT
                          1 Dec 2021, 08:14

                          @Christian-Ehrlicher

                          I think this topic is endless. Can you post some concrete example code?

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 1 Dec 2021, 10:41 last edited by
                          #14

                          @IknowQT said in QMap iterator Bug??:

                          I think this topic is endless. Can you post some concrete example code?

                          Why endless? All has been explained.
                          It is not a QMap iterator bug. It is your code which is wrong.
                          Each time you are calling m_pLogin->getAccountMgr() is returns a new QMap instance.
                          Your code has many errors:

                          • first : in the initialization statement in the for loop you are setting up iter is a begin of the return QMap.
                          • second: this return QMap is temporary, which means it is destroyed directly after usage ==> iter becomes a dangling pointer!!!
                          • third: in the condition statement, comparing iter with another QMap instance (which also is temporary!!)

                          Solution for this ==> learn C++ basics like variable/objects lifetime! (cf. https://en.cppreference.com/w/cpp/language/lifetime)

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          J 1 Reply Last reply 1 Dec 2021, 14:59
                          4
                          • K KroMignon
                            1 Dec 2021, 10:41

                            @IknowQT said in QMap iterator Bug??:

                            I think this topic is endless. Can you post some concrete example code?

                            Why endless? All has been explained.
                            It is not a QMap iterator bug. It is your code which is wrong.
                            Each time you are calling m_pLogin->getAccountMgr() is returns a new QMap instance.
                            Your code has many errors:

                            • first : in the initialization statement in the for loop you are setting up iter is a begin of the return QMap.
                            • second: this return QMap is temporary, which means it is destroyed directly after usage ==> iter becomes a dangling pointer!!!
                            • third: in the condition statement, comparing iter with another QMap instance (which also is temporary!!)

                            Solution for this ==> learn C++ basics like variable/objects lifetime! (cf. https://en.cppreference.com/w/cpp/language/lifetime)

                            J Offline
                            J Offline
                            JoeCFD
                            wrote on 1 Dec 2021, 14:59 last edited by JoeCFD 12 Jan 2021, 15:00
                            #15

                            @KroMignon if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well. But it is not great to code like he does. I think the following loop for map may be simpler

                                std::map<std::string, int > test_map;
                                test_map[ "test1" ] = 1;
                                test_map[ "test2" ] = 2;
                                for ( const std::pair< std::string, int > & p : test_map ) {
                                    std::cout << p.first << '\t' << p.second << std::endl;
                                }
                            
                            K 1 Reply Last reply 1 Dec 2021, 15:14
                            0
                            • J JoeCFD
                              1 Dec 2021, 14:59

                              @KroMignon if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well. But it is not great to code like he does. I think the following loop for map may be simpler

                                  std::map<std::string, int > test_map;
                                  test_map[ "test1" ] = 1;
                                  test_map[ "test2" ] = 2;
                                  for ( const std::pair< std::string, int > & p : test_map ) {
                                      std::cout << p.first << '\t' << p.second << std::endl;
                                  }
                              
                              K Offline
                              K Offline
                              KroMignon
                              wrote on 1 Dec 2021, 15:14 last edited by
                              #16

                              @JoeCFD said in QMap iterator Bug??:

                              if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well.

                              Yes, but that was not the point (I guess)

                              I think the following loop for map may be simpler

                              To be honest, there is no real difference. You are using std::map instead of QMap, that's all.
                              It is only a question of taste... Preferring std::map over QMap.
                              There is no need to use QMap::iterator to iterate through a QMap, it is only a possibility ;)

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              J 1 Reply Last reply 1 Dec 2021, 15:55
                              2
                              • K KroMignon
                                1 Dec 2021, 15:14

                                @JoeCFD said in QMap iterator Bug??:

                                if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well.

                                Yes, but that was not the point (I guess)

                                I think the following loop for map may be simpler

                                To be honest, there is no real difference. You are using std::map instead of QMap, that's all.
                                It is only a question of taste... Preferring std::map over QMap.
                                There is no need to use QMap::iterator to iterate through a QMap, it is only a possibility ;)

                                J Offline
                                J Offline
                                JoeCFD
                                wrote on 1 Dec 2021, 15:55 last edited by
                                #17

                                @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                                C K 2 Replies Last reply 1 Dec 2021, 16:00
                                0
                                • J JoeCFD
                                  1 Dec 2021, 15:55

                                  @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                                  C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 1 Dec 2021, 16:00 last edited by
                                  #18

                                  @JoeCFD said in QMap iterator Bug??:

                                  I believe

                                  ... so nonsense without any real background.
                                  And the above usecase which the topic is about (and your posts simply ignore) would be much slower with std::map than QMap since the contents of the std::map would have been copied.

                                  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
                                  • J JoeCFD
                                    1 Dec 2021, 15:55

                                    @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                                    K Offline
                                    K Offline
                                    KroMignon
                                    wrote on 1 Dec 2021, 16:02 last edited by
                                    #19

                                    @JoeCFD said in QMap iterator Bug??:

                                    I believe std containers may be better optimized than Qt containers in most cases.

                                    Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html
                                    or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    J 1 Reply Last reply 1 Dec 2021, 16:40
                                    4
                                    • K KroMignon
                                      1 Dec 2021, 16:02

                                      @JoeCFD said in QMap iterator Bug??:

                                      I believe std containers may be better optimized than Qt containers in most cases.

                                      Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html
                                      or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers

                                      J Offline
                                      J Offline
                                      JoeCFD
                                      wrote on 1 Dec 2021, 16:40 last edited by
                                      #20

                                      @KroMignon said in QMap iterator Bug??:

                                      @JoeCFD said in QMap iterator Bug??:

                                      I believe std containers may be better optimized than Qt containers in most cases.

                                      Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html <=== done in 2012 ages old.
                                      or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers read comments: 35

                                      C 1 Reply Last reply 1 Dec 2021, 16:52
                                      0
                                      • J JoeCFD
                                        1 Dec 2021, 16:40

                                        @KroMignon said in QMap iterator Bug??:

                                        @JoeCFD said in QMap iterator Bug??:

                                        I believe std containers may be better optimized than Qt containers in most cases.

                                        Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html <=== done in 2012 ages old.
                                        or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers read comments: 35

                                        C Offline
                                        C Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 1 Dec 2021, 16:52 last edited by Christian Ehrlicher 12 Jan 2021, 16:52
                                        #21

                                        @JoeCFD When you're at the point where you really need to worry about a speed difference between those implementations you should write your own implementation of the problematic container. You will never notice if a running program is using c++ or Qt or boost or whatever containers.

                                        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

                                        12/21

                                        1 Dec 2021, 08:14

                                        • Login

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