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. Deallocate memory to singleton class which access database ?
Forum Updated to NodeBB v4.3 + New Features

Deallocate memory to singleton class which access database ?

Scheduled Pinned Locked Moved Solved C++ Gurus
35 Posts 5 Posters 4.9k 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 jsulm
    21 Feb 2022, 13:25

    @Qt-embedded-developer As @KroMignon already asked you: do you have a C++11 capable compiler? And is C++11 activated?

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on 21 Feb 2022, 13:27 last edited by Qt embedded developer
    #18

    @jsulm no i have not. So what i have to change in below function:

    SqliteManagerBGSync::cleanup()
    {
    for (const auto& kv : sqliteManagerMap) {
        delete kv.second;
    }
    sqliteManagerMap.clear();
    
    }
    
    J 1 Reply Last reply 21 Feb 2022, 13:28
    0
    • Q Qt embedded developer
      21 Feb 2022, 13:21

      @KroMignon Hi i am not using qt . and sorry i can not enable C++ 11 because my compiler not support it. i can access map like std::map .

      K Offline
      K Offline
      KroMignon
      wrote on 21 Feb 2022, 13:27 last edited by KroMignon
      #19

      @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

      Hi i am not using qt . and sorry i can not enable C++ 11. i can access map like std::map .

      The problem is not std::map<>() but the way you iterate through the map: for (const auto& kv : sqliteManagerMap) requires at least C++11 support enabled! (cf. https://en.cppreference.com/w/cpp/container/map)

      I don't know which kind of C++ compiler you are using, for GCC for example enabling C++11 is done with -std=c++11 or -std=gnu++11

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

      Q 1 Reply Last reply 21 Feb 2022, 13:30
      3
      • Q Qt embedded developer
        21 Feb 2022, 13:27

        @jsulm no i have not. So what i have to change in below function:

        SqliteManagerBGSync::cleanup()
        {
        for (const auto& kv : sqliteManagerMap) {
            delete kv.second;
        }
        sqliteManagerMap.clear();
        
        }
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 21 Feb 2022, 13:28 last edited by
        #20

        @Qt-embedded-developer Then you can't use auto, write the type instead of auto

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

        1 Reply Last reply
        1
        • K KroMignon
          21 Feb 2022, 13:27

          @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

          Hi i am not using qt . and sorry i can not enable C++ 11. i can access map like std::map .

          The problem is not std::map<>() but the way you iterate through the map: for (const auto& kv : sqliteManagerMap) requires at least C++11 support enabled! (cf. https://en.cppreference.com/w/cpp/container/map)

          I don't know which kind of C++ compiler you are using, for GCC for example enabling C++11 is done with -std=c++11 or -std=gnu++11

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on 21 Feb 2022, 13:30 last edited by
          #21

          @KroMignon i can not enable it like that because any change like that not allowed in my device.

          J K 2 Replies Last reply 21 Feb 2022, 13:32
          0
          • Q Qt embedded developer
            21 Feb 2022, 13:30

            @KroMignon i can not enable it like that because any change like that not allowed in my device.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 21 Feb 2022, 13:32 last edited by
            #22

            @Qt-embedded-developer than do a normal for(x; x<y; ++x) loop


            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.

            Q 1 Reply Last reply 21 Feb 2022, 13:41
            1
            • J J.Hilk
              21 Feb 2022, 13:32

              @Qt-embedded-developer than do a normal for(x; x<y; ++x) loop

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on 21 Feb 2022, 13:41 last edited by
              #23

              @J-Hilk ,@jsulm ,@jsulm ,@KroMignon Thanks for enormous support.

              i have written like it below and now its not show any error:

              void SqliteManagerBGSync::cleanup()
              {
              for (short int i =0 ; i<sizeof(sqliteManagerMap);i++)
              {
                  delete sqliteManagerMap[i];
              }
              sqliteManagerMap.clear();
              
              }
              
              J 1 Reply Last reply 21 Feb 2022, 13:49
              0
              • Q Qt embedded developer
                21 Feb 2022, 13:41

                @J-Hilk ,@jsulm ,@jsulm ,@KroMignon Thanks for enormous support.

                i have written like it below and now its not show any error:

                void SqliteManagerBGSync::cleanup()
                {
                for (short int i =0 ; i<sizeof(sqliteManagerMap);i++)
                {
                    delete sqliteManagerMap[i];
                }
                sqliteManagerMap.clear();
                
                }
                
                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 21 Feb 2022, 13:49 last edited by
                #24

                @Qt-embedded-developer
                using sizeof is almost certainly wrong here.

                std::map has a size() function, please use that.
                https://en.cppreference.com/w/cpp/container/map/size


                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
                3
                • Q Qt embedded developer
                  21 Feb 2022, 13:30

                  @KroMignon i can not enable it like that because any change like that not allowed in my device.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 21 Feb 2022, 13:58 last edited by KroMignon
                  #25

                  @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                  i can not enable it like that because any change like that not allowed in my device.

                  As @jsulm already wrote before, replace auto with the corresponding type:

                  for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                  {
                  ...
                  }
                  

                  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 Q 3 Replies Last reply 21 Feb 2022, 14:01
                  1
                  • K KroMignon
                    21 Feb 2022, 13:58

                    @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                    i can not enable it like that because any change like that not allowed in my device.

                    As @jsulm already wrote before, replace auto with the corresponding type:

                    for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                    {
                    ...
                    }
                    
                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 21 Feb 2022, 14:01 last edited by J.Hilk
                    #26

                    @KroMignon IIRC range based for loops is a c++11 feature in itself 🤔

                    never mind, I think need a break!


                    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.

                    K 1 Reply Last reply 21 Feb 2022, 14:06
                    0
                    • J J.Hilk
                      21 Feb 2022, 14:01

                      @KroMignon IIRC range based for loops is a c++11 feature in itself 🤔

                      never mind, I think need a break!

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 21 Feb 2022, 14:06 last edited by
                      #27

                      @J-Hilk said in Deallocate memory to singleton class which access database ?:

                      IIRC range based for loops is a c++11 feature in itself 🤔

                      Are you sure? I don't believe that std::map<>::iterator support is comming with C++11.

                      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 21 Feb 2022, 14:09
                      0
                      • K KroMignon
                        21 Feb 2022, 14:06

                        @J-Hilk said in Deallocate memory to singleton class which access database ?:

                        IIRC range based for loops is a c++11 feature in itself 🤔

                        Are you sure? I don't believe that std::map<>::iterator support is comming with C++11.

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 21 Feb 2022, 14:09 last edited by
                        #28

                        @KroMignon I think @J-Hilk misunderstood your code: you are not using range based loop

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

                        1 Reply Last reply
                        1
                        • K KroMignon
                          21 Feb 2022, 13:58

                          @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                          i can not enable it like that because any change like that not allowed in my device.

                          As @jsulm already wrote before, replace auto with the corresponding type:

                          for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                          {
                          ...
                          }
                          
                          Q Offline
                          Q Offline
                          Qt embedded developer
                          wrote on 21 Feb 2022, 14:18 last edited by Qt embedded developer
                          #29

                          @KroMignon Here inside for loop i need to write

                          for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                          {
                          delete it;
                          }
                          
                          

                          or what i need to write to deallocate memory ?

                          J K 2 Replies Last reply 21 Feb 2022, 14:23
                          0
                          • Q Qt embedded developer
                            21 Feb 2022, 14:18

                            @KroMignon Here inside for loop i need to write

                            for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                            {
                            delete it;
                            }
                            
                            

                            or what i need to write to deallocate memory ?

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 21 Feb 2022, 14:23 last edited by
                            #30

                            @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                            delete it;

                            delete it->second;

                            of course

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

                            1 Reply Last reply
                            1
                            • Q Qt embedded developer
                              21 Feb 2022, 14:18

                              @KroMignon Here inside for loop i need to write

                              for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                              {
                              delete it;
                              }
                              
                              

                              or what i need to write to deallocate memory ?

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 21 Feb 2022, 14:25 last edited by KroMignon
                              #31

                              @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                              or what i need to write to deallocate ?

                              Please, take time to read std::map<> documentation.
                              You are using a map iterator, so it is a container object which gives you access to key and value.

                              delete it do not make sense!

                              You want to delete the pointer hold in the value part: delete it->second

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

                              Q 1 Reply Last reply 22 Feb 2022, 05:25
                              3
                              • K KroMignon
                                21 Feb 2022, 14:25

                                @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                                or what i need to write to deallocate ?

                                Please, take time to read std::map<> documentation.
                                You are using a map iterator, so it is a container object which gives you access to key and value.

                                delete it do not make sense!

                                You want to delete the pointer hold in the value part: delete it->second

                                Q Offline
                                Q Offline
                                Qt embedded developer
                                wrote on 22 Feb 2022, 05:25 last edited by Qt embedded developer
                                #32
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • K KroMignon
                                  21 Feb 2022, 13:58

                                  @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                                  i can not enable it like that because any change like that not allowed in my device.

                                  As @jsulm already wrote before, replace auto with the corresponding type:

                                  for(std::map<int,SqliteManagerBGSync*>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)
                                  {
                                  ...
                                  }
                                  
                                  Q Offline
                                  Q Offline
                                  Qt embedded developer
                                  wrote on 22 Feb 2022, 06:28 last edited by
                                  #33

                                  @KroMignon Hi when i put sqliteManagerMap::begin() and sqliteManagerMap::end() i am getting error .

                                  like below :

                                  for(std::map<int,SqliteManagerBGSync *>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)

                                  but when i put sqliteManagerMap.begin() then i am getting no error.

                                  J K 2 Replies Last reply 22 Feb 2022, 06:29
                                  0
                                  • Q Qt embedded developer
                                    22 Feb 2022, 06:28

                                    @KroMignon Hi when i put sqliteManagerMap::begin() and sqliteManagerMap::end() i am getting error .

                                    like below :

                                    for(std::map<int,SqliteManagerBGSync *>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)

                                    but when i put sqliteManagerMap.begin() then i am getting no error.

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 22 Feb 2022, 06:29 last edited by
                                    #34

                                    @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                                    sqliteManagerMap::begin() and sqliteManagerMap::end()

                                    As already explained in your other thread: begin() and end() are non-static methods, so you call them with . not :: !
                                    Please learn C++ and read documentation!

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

                                    1 Reply Last reply
                                    1
                                    • Q Qt embedded developer
                                      22 Feb 2022, 06:28

                                      @KroMignon Hi when i put sqliteManagerMap::begin() and sqliteManagerMap::end() i am getting error .

                                      like below :

                                      for(std::map<int,SqliteManagerBGSync *>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)

                                      but when i put sqliteManagerMap.begin() then i am getting no error.

                                      K Offline
                                      K Offline
                                      KroMignon
                                      wrote on 22 Feb 2022, 07:00 last edited by
                                      #35

                                      @Qt-embedded-developer said in Deallocate memory to singleton class which access database ?:

                                      for(std::map<int,SqliteManagerBGSync *>::iterator it = sqliteManagerMap::begin(); it != sqliteManagerMap::end(); ++it)

                                      Sorry my fault, as @jsulm already told, this are not static class methods. You want to call class instance methode:

                                      for(std::map<int,SqliteManagerBGSync *>::iterator it = sqliteManagerMap.begin(); it != sqliteManagerMap.end(); ++it)
                                      

                                      Was a long day yesterday!

                                      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

                                      27/35

                                      21 Feb 2022, 14:06

                                      • Login

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