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.
  • Q Qt embedded developer

    @jsulm @KroMignon Can anybody explain me how to free this map and memory allocated to instance ?

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

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

    Can you let me know how to free this map and memory allocated to instance ?

    As written before, there are 2 options:

    • use smart pointer, memory will be release when item is removed from map
    • call "manually" delete to free memory when item is removed from map.

    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 ?:

      Can you let me know how to free this map and memory allocated to instance ?

      As written before, there are 2 options:

      • use smart pointer, memory will be release when item is removed from map
      • call "manually" delete to free memory when item is removed from map.
      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by Qt embedded developer
      #7

      @KroMignon can you give example for my map. how to delete & deallocate memory to it ?

      jsulmJ 1 Reply Last reply
      0
      • Q Qt embedded developer

        @KroMignon can you give example for my map. how to delete & deallocate memory to it ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #8

        @Qt-embedded-developer What map? sqliteManagerMap? If so than there is no need to delete it as it is not allocated on the heap, apparently. To delete SqliteManagerBGSync instance use "delete" keyword as usual in C++ or use shared pointer as @KroMignon already suggested two times...

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

        Q 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Qt-embedded-developer What map? sqliteManagerMap? If so than there is no need to delete it as it is not allocated on the heap, apparently. To delete SqliteManagerBGSync instance use "delete" keyword as usual in C++ or use shared pointer as @KroMignon already suggested two times...

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

          @jsulm @KroMignon Hi i have access this instance based function in other class like below

          app::dbmanager::SqliteManagerBGSync::GetDBInstance()->openDatabase();

          And actually it is complex such that i am unable to understand how to delete it ?

          So i nee example to delete it.

          i have attached the code for openDatabase()

          int SqliteManagerBGSync::openDatabase()
          {
          	DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex locking");
          
          	if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
          	{
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase():: If 1");
          
          		pthread_mutex_lock(&databaseLock);
          	}
          	DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Locked");
          
          
          	if(handle != NULL){
          		DBGF_TRACE("AFCS:SqliteManagerBGSync::Handle success");
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
          		return SUCCESS;
          	}
          
          	int res = sqlite3_open(DB_PATH, &handle);
          	DBGF_TRACE("AFCS:SqliteManagerBGSync::sqlite3 open = %d",res);
          
          
          	DBGF_TRACE("AFCS:SqliteManagerBGSync::openDatabase::mutex lock called");
          	if(res != SQLITE_OK)
          	{
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Unlocking");
          		if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
          		{
          			DBGF_TRACE("SqliteManagerBGSync::openDatabase():: If 2");
          			pthread_mutex_unlock(&databaseLock);
          		}
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Unlocked");
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
          	    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
          		ERROR_RETURN(-1);
          	}else{
          		DBGF_TRACE("AFCS:SqliteManagerBGSync::openDatabase::Sqlite ok");
          
          		if(sqlite3_exec(handle, "PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;", NULL, 0, NULL) == SQLITE_OK)
          		{
          			DBGF_TRACE(" SQLITE QUERY SUCCESS: PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
          			DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
          		    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
          			return SUCCESS;
          		}
          		else
          		{
          			DBGF_TRACE(" SQLITE QUERY FAIL : PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
          			DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
          		    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
          			ERROR_RETURN(0);
          		}
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
          		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
          		return SUCCESS;
          	}
          
          }
          

          below i shown other class's function where i used above function :

          int CheckTotalErrorStep::FeatchTotalErrorCount()
          {
          	std::string strCurrentDate="0";
          	strCurrentDate = boomer::utils::datetime::getCurrentDateTime("YYYY-MM-DD");
          	DBGF_TRACE("AFCS:ReprintTicketStep::CheckNoOfTimesTryingToRePrintTicket()");
          	char *errMsg;
          	int result=0;
          	std::string usr_query="";
          	intTotalInsertedErrorLogCount = 0;
          	if(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->openDatabase() != SUCCESS)
          	{
          		ERROR_RETURN(0);
          	}
          	else
          	{
          		std::stringstream sql;
          
          		usr_query = "SELECT COUNT(INTERRORLOGID) FROM TBLERRORLOGS WHERE date(DTEERRORDATETIME) = '" + strCurrentDate + "';";
          
          		DBGF_TRACE("CheckTotalErrorStep::FeatchTotalErrorCount() : :usr_query=%s",usr_query);
          		result = sqlite3_exec(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->handle, usr_query.c_str(), &GetTotalErrorCount, 0, &errMsg);
          		DBGF_TRACE("AFCS:CheckTotalErrorStep::FeatchTotalErrorCount():result=%d",result);
          		if(result == SQLITE_OK)
          		{
          			app::dbmanager::SqliteManagerBGSync::GetDBInstance()->closeDatabase();
          			return SQLITE_OK;
          		}
          		else
          		{
          			DBGF_TRACE("CheckTotalErrorStep::FeatchTotalErrorCount() :Error=%s",sqlite3_errmsg(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->handle));
          			app::dbmanager::SqliteManagerBGSync::GetDBInstance()->closeDatabase();
          			return SQLITE_ERROR;
          		}
          	}
          	return result;
          }
          

          I am not using the QT. i am only using c++. i want to know shall i need to use shared pointer in map ?

          can anybody give example to deallocate memory for above singleton based class ?

          KroMignonK 1 Reply Last reply
          0
          • Q Qt embedded developer

            @jsulm @KroMignon Hi i have access this instance based function in other class like below

            app::dbmanager::SqliteManagerBGSync::GetDBInstance()->openDatabase();

            And actually it is complex such that i am unable to understand how to delete it ?

            So i nee example to delete it.

            i have attached the code for openDatabase()

            int SqliteManagerBGSync::openDatabase()
            {
            	DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex locking");
            
            	if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
            	{
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase():: If 1");
            
            		pthread_mutex_lock(&databaseLock);
            	}
            	DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Locked");
            
            
            	if(handle != NULL){
            		DBGF_TRACE("AFCS:SqliteManagerBGSync::Handle success");
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
            		return SUCCESS;
            	}
            
            	int res = sqlite3_open(DB_PATH, &handle);
            	DBGF_TRACE("AFCS:SqliteManagerBGSync::sqlite3 open = %d",res);
            
            
            	DBGF_TRACE("AFCS:SqliteManagerBGSync::openDatabase::mutex lock called");
            	if(res != SQLITE_OK)
            	{
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Unlocking");
            		if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
            		{
            			DBGF_TRACE("SqliteManagerBGSync::openDatabase():: If 2");
            			pthread_mutex_unlock(&databaseLock);
            		}
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase::Mutex Unlocked");
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
            	    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
            		ERROR_RETURN(-1);
            	}else{
            		DBGF_TRACE("AFCS:SqliteManagerBGSync::openDatabase::Sqlite ok");
            
            		if(sqlite3_exec(handle, "PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;", NULL, 0, NULL) == SQLITE_OK)
            		{
            			DBGF_TRACE(" SQLITE QUERY SUCCESS: PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
            			DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
            		    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
            			return SUCCESS;
            		}
            		else
            		{
            			DBGF_TRACE(" SQLITE QUERY FAIL : PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
            			DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
            		    DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
            			ERROR_RETURN(0);
            		}
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_max() = %d",_heap_max());
            		DBGF_TRACE("SqliteManagerBGSync::openDatabase() _heap_current() = %d",_heap_current());
            		return SUCCESS;
            	}
            
            }
            

            below i shown other class's function where i used above function :

            int CheckTotalErrorStep::FeatchTotalErrorCount()
            {
            	std::string strCurrentDate="0";
            	strCurrentDate = boomer::utils::datetime::getCurrentDateTime("YYYY-MM-DD");
            	DBGF_TRACE("AFCS:ReprintTicketStep::CheckNoOfTimesTryingToRePrintTicket()");
            	char *errMsg;
            	int result=0;
            	std::string usr_query="";
            	intTotalInsertedErrorLogCount = 0;
            	if(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->openDatabase() != SUCCESS)
            	{
            		ERROR_RETURN(0);
            	}
            	else
            	{
            		std::stringstream sql;
            
            		usr_query = "SELECT COUNT(INTERRORLOGID) FROM TBLERRORLOGS WHERE date(DTEERRORDATETIME) = '" + strCurrentDate + "';";
            
            		DBGF_TRACE("CheckTotalErrorStep::FeatchTotalErrorCount() : :usr_query=%s",usr_query);
            		result = sqlite3_exec(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->handle, usr_query.c_str(), &GetTotalErrorCount, 0, &errMsg);
            		DBGF_TRACE("AFCS:CheckTotalErrorStep::FeatchTotalErrorCount():result=%d",result);
            		if(result == SQLITE_OK)
            		{
            			app::dbmanager::SqliteManagerBGSync::GetDBInstance()->closeDatabase();
            			return SQLITE_OK;
            		}
            		else
            		{
            			DBGF_TRACE("CheckTotalErrorStep::FeatchTotalErrorCount() :Error=%s",sqlite3_errmsg(app::dbmanager::SqliteManagerBGSync::GetDBInstance()->handle));
            			app::dbmanager::SqliteManagerBGSync::GetDBInstance()->closeDatabase();
            			return SQLITE_ERROR;
            		}
            	}
            	return result;
            }
            

            I am not using the QT. i am only using c++. i want to know shall i need to use shared pointer in map ?

            can anybody give example to deallocate memory for above singleton based class ?

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

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

            And actually it is complex such that i am unable to understand how to delete it ?
            So i nee example to delete it.

            I don't understand what looks complicated to you.
            You have to delete the instance when you remove it from map.

            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 ?:

              And actually it is complex such that i am unable to understand how to delete it ?
              So i nee example to delete it.

              I don't understand what looks complicated to you.
              You have to delete the instance when you remove it from map.

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

              @KroMignon actually i have seen that already completely developed project never removed it. and never deleted it.

              as you can see that how i accessed GetAFCSTicketInstance() in other class.

              so where i need to put "delete SqliteManagerBGSync" ?

              jsulmJ KroMignonK 2 Replies Last reply
              0
              • Q Qt embedded developer

                @KroMignon actually i have seen that already completely developed project never removed it. and never deleted it.

                as you can see that how i accessed GetAFCSTicketInstance() in other class.

                so where i need to put "delete SqliteManagerBGSync" ?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

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

                so where i need to put "delete SqliteManagerBGSync" ?

                You could do it in the destructor of the class holding sqliteManagerMap member.

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

                Q 1 Reply Last reply
                1
                • Q Qt embedded developer

                  @KroMignon actually i have seen that already completely developed project never removed it. and never deleted it.

                  as you can see that how i accessed GetAFCSTicketInstance() in other class.

                  so where i need to put "delete SqliteManagerBGSync" ?

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

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

                  actually i have seen that already completely developed project never removed it. and never deleted it.
                  as you can see that how i accessed GetAFCSTicketInstance() in other class.
                  so where i need to put "delete SqliteManagerBGSync" ?

                  As far as I can see, app::dbmanager::SqliteManagerBGSync::GetDBInstance() seems to be a static method, so I guess the map is also an static member of class app::dbmanager::SqliteManagerBGSync.
                  To be totally clean, I would add a static method cleanup() to this class to clear properly the map:

                  for (const auto& kv : sqliteManagerMap) {
                      delete kv.second;
                  }
                  sqliteManagerMap.clear();
                  

                  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
                  • jsulmJ jsulm

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

                    so where i need to put "delete SqliteManagerBGSync" ?

                    You could do it in the destructor of the class holding sqliteManagerMap member.

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by
                    #14
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • KroMignonK KroMignon

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

                      actually i have seen that already completely developed project never removed it. and never deleted it.
                      as you can see that how i accessed GetAFCSTicketInstance() in other class.
                      so where i need to put "delete SqliteManagerBGSync" ?

                      As far as I can see, app::dbmanager::SqliteManagerBGSync::GetDBInstance() seems to be a static method, so I guess the map is also an static member of class app::dbmanager::SqliteManagerBGSync.
                      To be totally clean, I would add a static method cleanup() to this class to clear properly the map:

                      for (const auto& kv : sqliteManagerMap) {
                          delete kv.second;
                      }
                      sqliteManagerMap.clear();
                      
                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on last edited by
                      #15

                      @KroMignon as you can see from my function that i have taken the local map in function.so on scope get completed this map also get deleted. But there is i think 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 .

                      jsulmJ KroMignonK 2 Replies Last reply
                      0
                      • Q Qt embedded developer

                        @KroMignon as you can see from my function that i have taken the local map in function.so on scope get completed this map also get deleted. But there is i think 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 .

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

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

                        delete SqliteManagerBGSync() ;

                        Does not make sense and is not valid C++ - you are trying to delete a new stack allocated SqliteManagerBGSync instance...

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

                        Q 1 Reply Last reply
                        1
                        • Q Qt embedded developer

                          @KroMignon as you can see from my function that i have taken the local map in function.so on scope get completed this map also get deleted. But there is i think 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 .

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

                          @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?

                          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
                          • jsulmJ jsulm

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

                            delete SqliteManagerBGSync() ;

                            Does not make sense and is not valid C++ - you are trying to delete a new stack allocated SqliteManagerBGSync instance...

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

                            @jsulm So then just let me know this local map allocated the dynamic memory to class when and how to delete it with example. still i am misinterpret it then let me know.

                            1 Reply Last reply
                            0
                            • 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

                                          • Login

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