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. Can any body explain me below statement of function ?
Forum Updated to NodeBB v4.3 + New Features

Can any body explain me below statement of function ?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
30 Posts 4 Posters 8.1k 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.
  • KroMignonK KroMignon

    @Qt-embedded-developer said in Can any body explain me below statement of function ?:

    SqliteManagerBGSync() is global which get allocated to memory so i think shall i need to use
    delete SqliteManagerBGSync() ; in cleanup() memthod.
    May be i am wrong. if it is then please correct me .

    I don't want to hurt, just to know how to help you, but what are your programming skills?
    Do you know what are static methods or classes?
    According to you code extract, SqliteManagerBGSync is a class.
    I guess method GetDBInstance() is declared in header as follow:

    class SqliteManagerBGSync
    {
    public:
        static SqliteManagerBGSync* GetDBInstance();
    };
    

    Correct or wrong?

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #19

    @KroMignon yes it is static. thanks. just let me help me how to make this code proper.

    Because i have see that my application is get restarted due too much heap utilization.

    and i think this above code is reason for that.

    KroMignonK 1 Reply Last reply
    0
    • Q Qt embedded developer

      @KroMignon yes it is static. thanks. just let me help me how to make this code proper.

      Because i have see that my application is get restarted due too much heap utilization.

      and i think this above code is reason for that.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #20

      @Qt-embedded-developer said in Can any body explain me below statement of function ?:

      yes it is static. thanks. just let me help me how to make this code proper.
      Because i have see that my application is get restarted due too much heap utilization.
      and i think this above code is reason for that.

      This code don't look bad to me and I don't think it is the place where heap memory is consuming.
      As written before, it will only ensure that you are using the right SqliteManagerBGSync for the current thread and create a new SqliteManagerBGSync if there is no one already created for current thread.

      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
      0
      • KroMignonK KroMignon

        @Qt-embedded-developer said in Can any body explain me below statement of function ?:

        yes it is static. thanks. just let me help me how to make this code proper.
        Because i have see that my application is get restarted due too much heap utilization.
        and i think this above code is reason for that.

        This code don't look bad to me and I don't think it is the place where heap memory is consuming.
        As written before, it will only ensure that you are using the right SqliteManagerBGSync for the current thread and create a new SqliteManagerBGSync if there is no one already created for current thread.

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #21

        @KroMignon means as my static function consist map with local scope. if is okay then i not need to clear it. But query is that though one copy of class member will have allocated memory to class. So whenever it to some task it will occupy some memory. so that memory when to and how to free it ?

        KroMignonK 1 Reply Last reply
        0
        • Q Qt embedded developer

          @KroMignon means as my static function consist map with local scope. if is okay then i not need to clear it. But query is that though one copy of class member will have allocated memory to class. So whenever it to some task it will occupy some memory. so that memory when to and how to free it ?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #22

          @Qt-embedded-developer said in Can any body explain me below statement of function ?:

          means as my static function consist map with local scope. if is okay then i not need to clear it. But query is that allocated memory to class when to and how to free it ?

          I don't believe the map sqliteManagerMap is local, the map must also be static.

          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
          0
          • KroMignonK KroMignon

            @Qt-embedded-developer said in Can any body explain me below statement of function ?:

            means as my static function consist map with local scope. if is okay then i not need to clear it. But query is that allocated memory to class when to and how to free it ?

            I don't believe the map sqliteManagerMap is local, the map must also be static.

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #23

            @KroMignon means i can not free the memory ?

            as my function is static, so map taken inside this function is also static and memory allocated to this class also static. if this is true then allocated memory will use heap segment or data segment.

            i know as i am unsure about many things it make frustrating for other. sorry for that. Just i want to improve my self.

            KroMignonK 1 Reply Last reply
            0
            • Q Qt embedded developer

              @KroMignon means i can not free the memory ?

              as my function is static, so map taken inside this function is also static and memory allocated to this class also static. if this is true then allocated memory will use heap segment or data segment.

              i know as i am unsure about many things it make frustrating for other. sorry for that. Just i want to improve my self.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #24

              @Qt-embedded-developer said in Can any body explain me below statement of function ?:

              as my function is static, so map taken inside this function is also static and memory allocated to this class also static. if this is true then allocated memory will use heap segment or data segment.
              i know as i am unsure about many things it make frustrating for other. sorry for that. Just i want to improve my self.

              Please, take time to understand memory allocation and variable/class member scope!
              This is very basic C++ knowledge you have to know to be able to program.
              There is no magic.

              In C++, a static class method can only access to static class members or to members of class instances (or global variable of course).
              So if GetDBInstance() can have access to sqliteManagerMap, this variable must be static or global. That is all I have say.

              I am pretty sure; sqliteManagerMap is declared is class header with something like this:

              class SqliteManagerBGSync
              {
              protected: 
                  static std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
              };
              

              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
              1
              • KroMignonK KroMignon

                @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                as my function is static, so map taken inside this function is also static and memory allocated to this class also static. if this is true then allocated memory will use heap segment or data segment.
                i know as i am unsure about many things it make frustrating for other. sorry for that. Just i want to improve my self.

                Please, take time to understand memory allocation and variable/class member scope!
                This is very basic C++ knowledge you have to know to be able to program.
                There is no magic.

                In C++, a static class method can only access to static class members or to members of class instances (or global variable of course).
                So if GetDBInstance() can have access to sqliteManagerMap, this variable must be static or global. That is all I have say.

                I am pretty sure; sqliteManagerMap is declared is class header with something like this:

                class SqliteManagerBGSync
                {
                protected: 
                    static std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
                };
                
                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by Qt embedded developer
                #25

                @KroMignon said in Can any body explain me below statement of function ?:

                static std::map<int, SqliteManagerBGSync*> sqliteManagerMap;

                Yes its static.

                what happen when this map not declared as static and this map declared inside the static function defination only ?

                JonBJ 1 Reply Last reply
                0
                • Q Qt embedded developer

                  @KroMignon said in Can any body explain me below statement of function ?:

                  static std::map<int, SqliteManagerBGSync*> sqliteManagerMap;

                  Yes its static.

                  what happen when this map not declared as static and this map declared inside the static function defination only ?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #26

                  @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                  static function defination

                  A "static function defination" simply means the function/method exists outside any instance of a class. It has no implication for storage. std::map<int, SqliteManagerBGSync*> sqliteManagerMap; (no static) would be just a local variable in the function, destroyed after the function call completes.

                  If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class. But then different instances of the class have separate maps, they do not share a single map.

                  Q 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                    static function defination

                    A "static function defination" simply means the function/method exists outside any instance of a class. It has no implication for storage. std::map<int, SqliteManagerBGSync*> sqliteManagerMap; (no static) would be just a local variable in the function, destroyed after the function call completes.

                    If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class. But then different instances of the class have separate maps, they do not share a single map.

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by Qt embedded developer
                    #27

                    @JonB said in Can any body explain me below statement of function ?:

                    declare it as a (non-static) member variable in the class

                    i think above sentence is incorrect. it may be static instead non-static.

                    JonBJ 1 Reply Last reply
                    0
                    • Q Qt embedded developer

                      @JonB said in Can any body explain me below statement of function ?:

                      declare it as a (non-static) member variable in the class

                      i think above sentence is incorrect. it may be static instead non-static.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #28

                      @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                      i think above sentence is incorrect.

                      Pardon? Member variables might be static or not. With different lifetime correspondingly. You asked about static std::map<int, SqliteManagerBGSync*> sqliteManagerMap; "when this map not declared as static ", which I answered. I wrote:

                      If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class

                      which is correct. May or may not be what you want, but it's correct....

                      Q 1 Reply Last reply
                      4
                      • JonBJ JonB

                        @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                        i think above sentence is incorrect.

                        Pardon? Member variables might be static or not. With different lifetime correspondingly. You asked about static std::map<int, SqliteManagerBGSync*> sqliteManagerMap; "when this map not declared as static ", which I answered. I wrote:

                        If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class

                        which is correct. May or may not be what you want, but it's correct....

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #29

                        @JonB Thanks

                        1 Reply Last reply
                        0
                        • KroMignonK KroMignon

                          @Qt-embedded-developer said in Can any body explain me below statement of function ?:

                          i want to know the what is technical meaning of below statement
                          sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync>(threadId,new SqliteManagerBGSync()));
                          shall i need to free memory allocated to SqliteManagerBGSync() ?

                          I think the idea between this is to get access to DB connection which has been created for the current thread (as QSqlDatabase has to be used in the thread in which it has been created).

                          But to answer to your question, yes you have to free used memory.
                          The easiest way to do it, would be to use smart pointer, like QSharedPointer ==> use QSharedPointer<SqliteManagerBGSync> instead of SqliteManagerBGSync*.

                          Q Offline
                          Q Offline
                          Qt embedded developer
                          wrote on last edited by
                          #30

                          @KroMignon thanks for suggesting smart pointer.

                          1 Reply Last reply
                          0

                          • Login

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