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 7.0k 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

    I have seen that my project code is use singleton class to access database.

    I want to free the memory allocated by this class how to do it ? can any body give example for that ?

    #ifndef SQLMANAGER_H_
    #define SQLMANAGER_H_
    
    #include <boomer.h>
    #include <sqlite/sqlite3.h>
    #include <vector>
    #include <string>
    #include "Header/AFCSTicket.h"
    //#include "Error.h"
    
    using namespace std;
    
    namespace app
    {
    namespace dbmanager
    {
    
    class SqliteManager {
    
    public :
    	static	SqliteManager* GetDBInstance();
    	sqlite3* handle;
    	sqlite3_stmt *statement;
    	static sqlite3* getDataBaseHandle();
    
    	int prepareStatement(const char *query);
    	int openDatabase();
    	void closeDatabase();
    	boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle);
    	boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle);
    	void closeDatabaseForDBBackup();
    private:
    	SqliteManager();
    	static SqliteManager* iSqlInstanceDB;
    
    };
    }
    
    }
    #endif
    
    
    
    #include "SqliteManager.h"
    #include <stdio.h>
    #include <string>
    
    
    using namespace std;
    
    using namespace boomer::error;
    
    const char* DB_PATH_MAIN = "CONFIG.DB";
    
    namespace app
    {
    namespace dbmanager
    {
    SqliteManager* SqliteManager::iSqlInstanceDB = NULL;
    SqliteManager::SqliteManager()
    {
    	handle = NULL;
    }
    
    SqliteManager* SqliteManager::GetDBInstance()
    {
    	DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
    	DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
    
    	if ( iSqlInstanceDB == NULL ) {
    		iSqlInstanceDB = new SqliteManager();
    		DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
    	  DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
    	}
    	else
    	{
    	DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
    	DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
    	}
    	return iSqlInstanceDB;
    }
    
    int SqliteManager::openDatabase()
    {
    	int memused = sqlite3_memory_used();
        DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
    
    	if(handle != NULL)
    	{
    		DBGF_TRACE("before SqliteManager::openDatabase _heap_max() = %d",_heap_max());
    		DBGF_TRACE("before SqliteManager::openDatabase _heap_current() = %d",_heap_current());
    
    
    		int val = sqlite3_db_release_memory(handle);
    
    	    memused = sqlite3_memory_used();
    		DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
    
    		DBGF_TRACE("SqliteManager::openDatabase sqlite3_db_cacheflush val = %d",val);
    
    		DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max());
    		DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current());
    		return SUCCESS;
    	}
    
    	int res = sqlite3_open(DB_PATH_MAIN, &handle);
    	if(res != SQLITE_OK)
    	{
      	DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max());
    		DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current());
    		ERROR_RETURN(0);
    	}
    	else
    	{
    		DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
    	  DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
    
    	    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("openDatabase _heap_max() = %d",_heap_max());
    		  DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
    			return SUCCESS;
    		}
    		else
    		{
    			DBGF_TRACE("SQLITE QUERY FAIL: PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
    			DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
    			DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
    
    			ERROR_RETURN(0);
    		}
    		DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
    		DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
    		return SUCCESS;
    	}
    
    }
    
    int SqliteManager::prepareStatement(const char *query)
    {
    	if(openDatabase() != SUCCESS)
    	{
    		ERROR_RETURN(0);
    	}
    
    	statement = NULL;
    	int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0);
    
    	if(res != SQLITE_OK)
    	{
    		sqlite3_finalize(statement);
    		statement = NULL;
    
    		ERROR_RETURN(0);
    
    	}else{
    		return SUCCESS;
    	}
    }
    
    
    void SqliteManager::closeDatabase()
    {
    	DBGF_TRACE("_heap_max() = %d",_heap_max());
        DBGF_TRACE("_heap_current() = %d",_heap_current());
    	//	if (handle != NULL)
    	//	{
    	//		if(sqlite3_close(handle) == SQLITE_OK)
    	//		{
    	//			DBGF_TRACE("Close Database Success: %s", sqlite3_errmsg(handle));
    	//			handle = NULL;
    	//		}
    	//		else
    	//			LOGF_ERROR("Close Database Error: %s", sqlite3_errmsg(handle));
    	//	}
    }
    
    boomer::transaction::TransactionResult SqliteManager::connectToDataBase(std::string dbFilename , sqlite3* &handle)
    {
    	int res = 0;
    	res = sqlite3_open(dbFilename.c_str(), &handle);
    	if( res != SQLITE_OK )
    	{
    		ERROR_RETURN(CONFIG_DB_CONNECT_FAILED);
    	}
    
    	return boomer::transaction::trOk;
    }
    
    boomer::transaction::TransactionResult SqliteManager::disconnectDataBase(sqlite3* &handle)
    {
    	long errCode = 0;
    	//Disconnect to DataBase
    	int res = sqlite3_close(handle);
    	if(res)
    	{
    		errCode = CONFIG_DB_DISCONNECT_FAILED;
    		return res;
    	}
    	handle = NULL;
    	return boomer::transaction::trOk;
    }
    void SqliteManager::closeDatabaseForDBBackup()
    {
    	DBGF_TRACE("AFCS:SqliteManager::closeDatabaseForDBBackup()");
    	if (handle != NULL)
    	{
    		if(sqlite3_close(handle) == SQLITE_OK)
    		{
    			DBGF_TRACE("closeDatabaseForDBBackup::Close Database Success: %s", sqlite3_errmsg(handle));
    			handle = NULL;
    		}
    		else
    		{
    			//LOGF_ERROR("closeDatabaseForDBBackup::Close Database Error: %s", sqlite3_errmsg(handle));
    		}
    	}
    }
    
    }
    
    }
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

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

    I want to free the memory allocated by this class how to do it ?

    As usual with heap allocated memory using delete:

    delete iSqlInstanceDB;
    

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

    Q 1 Reply Last reply
    3
    • jsulmJ jsulm

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

      I want to free the memory allocated by this class how to do it ?

      As usual with heap allocated memory using delete:

      delete iSqlInstanceDB;
      
      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by Qt embedded developer
      #3

      @jsulm I want to know how to deallocate memory if my class implemented like below :

      /*
       * SqliteManagerBGSync.h
       *
       *  Created on: 16-Jul-2018
       *      Author: ankita.singh
       */
      
      #ifndef TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_
      #define TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_
      
      #include <boomer.h>
      #include <sqlite/sqlite3.h>
      #include <vector>
      #include <string>
      #include <pthread.h>
      #include "Header/AFCSTicket.h"
      
      namespace app {
      namespace dbmanager {
      
      class SqliteManagerBGSync {
      public:
      
      public :
      	static std::map<int,SqliteManagerBGSync *> sqliteManagerMap;
      	static	SqliteManagerBGSync* GetDBInstance();
      	sqlite3* handle;
      	sqlite3_stmt *statement;
      	static sqlite3* getDataBaseHandle();
      
      	static SqliteManagerBGSync* GetDBInstanceForBackSync();
      	int prepareStatement(const char *query);
      	int openDatabase();
      	void closeDatabase();
      	boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle);
      	boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle);
      	void closeDatabaseForDBBackup();
      
      private:
      	std::map<int,sqlite3 *> sqliteHandleMap;
      	SqliteManagerBGSync();
      	cleanup();
      
      };
      
      } /* namespace dbmanager */
      } /* namespace app */
      
      #endif /* TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_ */
      
      
      #include "SqliteManagerBGSync.h"
      #include <stdio.h>
      #include <string>
      
      
      using namespace std;
      
      using namespace boomer::error;
      
      const char* DB_PATH = "TICKET.DB";
      pthread_mutex_t databaseLock;
      
      
      namespace app
      {
      namespace dbmanager
      {
      std::map<int,SqliteManagerBGSync *> SqliteManagerBGSync::sqliteManagerMap;
      SqliteManagerBGSync::SqliteManagerBGSync()
      {
      	DBGF_TRACE("AFCS:SqliteManagerBGSync::Constructor Called");
      	handle = NULL;
      }
      
      SqliteManagerBGSync::cleanup()
      {
      for (const auto& kv : sqliteManagerMap) {
          delete kv.second;
      }
      sqliteManagerMap.clear();
      
      }
      
      SqliteManagerBGSync* SqliteManagerBGSync::GetDBInstance()
      {
      	std::map<int,SqliteManagerBGSync*>::iterator it;
      	int threadId = pthread_self();
      	DBGF_TRACE("AFCS:SqliteManagerBGSync::thread Id :%d",threadId);
      
      	//sqliteManagerMap.empty() Return 0 If sqliteManagerMap is Not Empty
      	//Return 1 If sqliteManagerMap is Empty
      	DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty()=%d",sqliteManagerMap.empty());
      
      	if(sqliteManagerMap.empty())
      	{
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty() IF");
      		if(pthread_mutex_init(&databaseLock, NULL) != 0)
      		{
      			DBGF_TRACE("databaseLock mutex init failed");
      
      		}
      		else
      		{
      			DBGF_TRACE("databaseLock mutex init Success");
      
      		}
      	}
      //	else
      //	{
      //		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty() Else");
      //		sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync*>(threadId,new SqliteManagerBGSync()));
      //		it = sqliteManagerMap.find(threadId);
      //		DBGF_TRACE("AFCS:SqliteManagerBGSync::sqliteManagerMap success return 1");
      //		return it->second;
      //	}
      
      	it = sqliteManagerMap.find(threadId);
      	if(it == sqliteManagerMap.end())
      	{
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::threadId Find Success");
      
      		sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync*>(threadId,new SqliteManagerBGSync()));
      		DBGF_TRACE("AFCS:sqliteManagerMap insert called");
      
      		it = sqliteManagerMap.find(threadId);
      	}
      	DBGF_TRACE("AFCS:SqliteManagerBGSync::sqliteManagerMap success return 2");
      
      	DBGF_TRACE("SqliteManagerBGSync::GetDBInstance _heap_max() = %d",_heap_max());
      	DBGF_TRACE("SqliteManagerBGSync::GetDBInstance _heap_current() = %d",_heap_current());
      
      	return it->second;
      }
      
      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");
      	int memused = sqlite3_memory_used();
      	DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
      
      	if(handle != NULL){
      
      		DBGF_TRACE("before SqliteManagerBGSync::openDatabase _heap_max() = %d",_heap_max());
      		DBGF_TRACE("before SqliteManagerBGSync::openDatabase _heap_current() = %d",_heap_current());
      
      		int val = sqlite3_db_release_memory(handle);
      
      		memused = sqlite3_memory_used();
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::memused=%d",memused);
      
      		DBGF_TRACE("SqliteManagerBGSync::openDatabase sqlite3_db_cacheflush val = %d",val);
      
      		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;
      	}
      
      }
      
      int SqliteManagerBGSync::prepareStatement(const char *query)
      {
      	if(openDatabase() != SUCCESS)
      	{
      		ERROR_RETURN(-1);
      	}
      
      	statement = NULL;
      	int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0);
      
      	if(res != SQLITE_OK)
      	{
      		sqlite3_finalize(statement);
      		statement = NULL;
      		DBGF_TRACE("Mutex Unlocking");
      		pthread_mutex_unlock(&databaseLock);
      		DBGF_TRACE("Mutex Unlocked");
      		ERROR_RETURN(-1);
      
      	}else{
      		return SUCCESS;
      	}
      }
      
      
      void SqliteManagerBGSync::closeDatabase()
      {
      	DBGF_TRACE("AFCS:close database called");
      	//SVC_WAIT(1000);
      
      	DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
      	DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
      
      	if (handle != NULL)
      	{
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():IF");
      		int intReleaseMemoryInBG = 0,intBeforeReleaseInBG=0,intAfterReleaseInBG=0;
      
      		intBeforeReleaseInBG = sqlite3_memory_used();
      		intReleaseMemoryInBG = sqlite3_db_release_memory(handle);
      		intAfterReleaseInBG = sqlite3_memory_used();
      
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intReleaseMemoryInBG=%d",intReleaseMemoryInBG);
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intBeforeReleaseInBG=%d",intBeforeReleaseInBG);
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intAfterReleaseInBG=%d",intAfterReleaseInBG);
      
      		int res = sqlite3_close(handle);
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::res=%d",res);
      		if( res == SQLITE_OK)
      		{
      			DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::Close Database Success: %s", sqlite3_errmsg(handle));
      			//SVC_WAIT(1000);
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase()::Mutex Unlocking");
      			if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
      			{
      				DBGF_TRACE("SqliteManagerBGSync::closeDatabase():: If 1");
      				pthread_mutex_unlock(&databaseLock);
      			}
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase()::Mutex Unlocked");
      			handle = NULL;
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase():: Set handle To NULL");
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
      		}
      		else
      		{
      			DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::Close Database Fail: %s", sqlite3_errmsg(handle));
      			//LOGF_ERROR("AFCS:Close Database Error: %s", sqlite3_errmsg(handle));
      			//SVC_WAIT(1000);
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
      			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
      		}
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():Complete IF");
      	}
      	else
      	{
      		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():Else");
      		DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
      		DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
      	}
      	cleanup();
      }
      
      boomer::transaction::TransactionResult SqliteManagerBGSync::connectToDataBase(std::string dbFilename , sqlite3* &handle)
      {
      	int res = 0;
      	res = sqlite3_open(dbFilename.c_str(), &handle);
      	if( res != SQLITE_OK )
      	{
      		ERROR_RETURN(CONFIG_DB_CONNECT_FAILED);
      	}
      
      	return boomer::transaction::trOk;
      }
      
      boomer::transaction::TransactionResult SqliteManagerBGSync::disconnectDataBase(sqlite3* &handle)
      {
      	long errCode = 0;
      	//Disconnect to DataBase
      	int res = sqlite3_close(handle);
      	if(res)
      	{
      		errCode = CONFIG_DB_DISCONNECT_FAILED;
      		return res;
      	}
      	handle = NULL;
      	return boomer::transaction::trOk;
      }
      
      }
      
      }
      
      
      JonBJ 1 Reply Last reply
      0
      • Q Qt embedded developer

        @jsulm I want to know how to deallocate memory if my class implemented like below :

        /*
         * SqliteManagerBGSync.h
         *
         *  Created on: 16-Jul-2018
         *      Author: ankita.singh
         */
        
        #ifndef TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_
        #define TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_
        
        #include <boomer.h>
        #include <sqlite/sqlite3.h>
        #include <vector>
        #include <string>
        #include <pthread.h>
        #include "Header/AFCSTicket.h"
        
        namespace app {
        namespace dbmanager {
        
        class SqliteManagerBGSync {
        public:
        
        public :
        	static std::map<int,SqliteManagerBGSync *> sqliteManagerMap;
        	static	SqliteManagerBGSync* GetDBInstance();
        	sqlite3* handle;
        	sqlite3_stmt *statement;
        	static sqlite3* getDataBaseHandle();
        
        	static SqliteManagerBGSync* GetDBInstanceForBackSync();
        	int prepareStatement(const char *query);
        	int openDatabase();
        	void closeDatabase();
        	boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle);
        	boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle);
        	void closeDatabaseForDBBackup();
        
        private:
        	std::map<int,sqlite3 *> sqliteHandleMap;
        	SqliteManagerBGSync();
        	cleanup();
        
        };
        
        } /* namespace dbmanager */
        } /* namespace app */
        
        #endif /* TRANSIT_SRC_SQLITEMANAGERBGSYNC_H_ */
        
        
        #include "SqliteManagerBGSync.h"
        #include <stdio.h>
        #include <string>
        
        
        using namespace std;
        
        using namespace boomer::error;
        
        const char* DB_PATH = "TICKET.DB";
        pthread_mutex_t databaseLock;
        
        
        namespace app
        {
        namespace dbmanager
        {
        std::map<int,SqliteManagerBGSync *> SqliteManagerBGSync::sqliteManagerMap;
        SqliteManagerBGSync::SqliteManagerBGSync()
        {
        	DBGF_TRACE("AFCS:SqliteManagerBGSync::Constructor Called");
        	handle = NULL;
        }
        
        SqliteManagerBGSync::cleanup()
        {
        for (const auto& kv : sqliteManagerMap) {
            delete kv.second;
        }
        sqliteManagerMap.clear();
        
        }
        
        SqliteManagerBGSync* SqliteManagerBGSync::GetDBInstance()
        {
        	std::map<int,SqliteManagerBGSync*>::iterator it;
        	int threadId = pthread_self();
        	DBGF_TRACE("AFCS:SqliteManagerBGSync::thread Id :%d",threadId);
        
        	//sqliteManagerMap.empty() Return 0 If sqliteManagerMap is Not Empty
        	//Return 1 If sqliteManagerMap is Empty
        	DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty()=%d",sqliteManagerMap.empty());
        
        	if(sqliteManagerMap.empty())
        	{
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty() IF");
        		if(pthread_mutex_init(&databaseLock, NULL) != 0)
        		{
        			DBGF_TRACE("databaseLock mutex init failed");
        
        		}
        		else
        		{
        			DBGF_TRACE("databaseLock mutex init Success");
        
        		}
        	}
        //	else
        //	{
        //		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::sqliteManagerMap.empty() Else");
        //		sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync*>(threadId,new SqliteManagerBGSync()));
        //		it = sqliteManagerMap.find(threadId);
        //		DBGF_TRACE("AFCS:SqliteManagerBGSync::sqliteManagerMap success return 1");
        //		return it->second;
        //	}
        
        	it = sqliteManagerMap.find(threadId);
        	if(it == sqliteManagerMap.end())
        	{
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::GetDBInstance()::threadId Find Success");
        
        		sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync*>(threadId,new SqliteManagerBGSync()));
        		DBGF_TRACE("AFCS:sqliteManagerMap insert called");
        
        		it = sqliteManagerMap.find(threadId);
        	}
        	DBGF_TRACE("AFCS:SqliteManagerBGSync::sqliteManagerMap success return 2");
        
        	DBGF_TRACE("SqliteManagerBGSync::GetDBInstance _heap_max() = %d",_heap_max());
        	DBGF_TRACE("SqliteManagerBGSync::GetDBInstance _heap_current() = %d",_heap_current());
        
        	return it->second;
        }
        
        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");
        	int memused = sqlite3_memory_used();
        	DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
        
        	if(handle != NULL){
        
        		DBGF_TRACE("before SqliteManagerBGSync::openDatabase _heap_max() = %d",_heap_max());
        		DBGF_TRACE("before SqliteManagerBGSync::openDatabase _heap_current() = %d",_heap_current());
        
        		int val = sqlite3_db_release_memory(handle);
        
        		memused = sqlite3_memory_used();
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::memused=%d",memused);
        
        		DBGF_TRACE("SqliteManagerBGSync::openDatabase sqlite3_db_cacheflush val = %d",val);
        
        		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;
        	}
        
        }
        
        int SqliteManagerBGSync::prepareStatement(const char *query)
        {
        	if(openDatabase() != SUCCESS)
        	{
        		ERROR_RETURN(-1);
        	}
        
        	statement = NULL;
        	int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0);
        
        	if(res != SQLITE_OK)
        	{
        		sqlite3_finalize(statement);
        		statement = NULL;
        		DBGF_TRACE("Mutex Unlocking");
        		pthread_mutex_unlock(&databaseLock);
        		DBGF_TRACE("Mutex Unlocked");
        		ERROR_RETURN(-1);
        
        	}else{
        		return SUCCESS;
        	}
        }
        
        
        void SqliteManagerBGSync::closeDatabase()
        {
        	DBGF_TRACE("AFCS:close database called");
        	//SVC_WAIT(1000);
        
        	DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
        	DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
        
        	if (handle != NULL)
        	{
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():IF");
        		int intReleaseMemoryInBG = 0,intBeforeReleaseInBG=0,intAfterReleaseInBG=0;
        
        		intBeforeReleaseInBG = sqlite3_memory_used();
        		intReleaseMemoryInBG = sqlite3_db_release_memory(handle);
        		intAfterReleaseInBG = sqlite3_memory_used();
        
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intReleaseMemoryInBG=%d",intReleaseMemoryInBG);
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intBeforeReleaseInBG=%d",intBeforeReleaseInBG);
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::intAfterReleaseInBG=%d",intAfterReleaseInBG);
        
        		int res = sqlite3_close(handle);
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::res=%d",res);
        		if( res == SQLITE_OK)
        		{
        			DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::Close Database Success: %s", sqlite3_errmsg(handle));
        			//SVC_WAIT(1000);
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase()::Mutex Unlocking");
        			if(app::Header::AFCSTicket::GetAFCSTicketInstance()->getDBClosed()!=0)
        			{
        				DBGF_TRACE("SqliteManagerBGSync::closeDatabase():: If 1");
        				pthread_mutex_unlock(&databaseLock);
        			}
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase()::Mutex Unlocked");
        			handle = NULL;
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase():: Set handle To NULL");
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
        		}
        		else
        		{
        			DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase()::Close Database Fail: %s", sqlite3_errmsg(handle));
        			//LOGF_ERROR("AFCS:Close Database Error: %s", sqlite3_errmsg(handle));
        			//SVC_WAIT(1000);
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
        			DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
        		}
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():Complete IF");
        	}
        	else
        	{
        		DBGF_TRACE("AFCS:SqliteManagerBGSync::closeDatabase():Else");
        		DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_max() = %d",_heap_max());
        		DBGF_TRACE("SqliteManagerBGSync::closeDatabase() _heap_current() = %d",_heap_current());
        	}
        	cleanup();
        }
        
        boomer::transaction::TransactionResult SqliteManagerBGSync::connectToDataBase(std::string dbFilename , sqlite3* &handle)
        {
        	int res = 0;
        	res = sqlite3_open(dbFilename.c_str(), &handle);
        	if( res != SQLITE_OK )
        	{
        		ERROR_RETURN(CONFIG_DB_CONNECT_FAILED);
        	}
        
        	return boomer::transaction::trOk;
        }
        
        boomer::transaction::TransactionResult SqliteManagerBGSync::disconnectDataBase(sqlite3* &handle)
        {
        	long errCode = 0;
        	//Disconnect to DataBase
        	int res = sqlite3_close(handle);
        	if(res)
        	{
        		errCode = CONFIG_DB_DISCONNECT_FAILED;
        		return res;
        	}
        	handle = NULL;
        	return boomer::transaction::trOk;
        }
        
        }
        
        }
        
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #4

        @Qt-embedded-developer
        This is hundreds of lines of code. Nobody else wants to read through all your code, it's not reasonable to ask them to do so. Please produce a minimal example if you have a question.

        Q 1 Reply Last reply
        1
        • JonBJ JonB

          @Qt-embedded-developer
          This is hundreds of lines of code. Nobody else wants to read through all your code, it's not reasonable to ask them to do so. Please produce a minimal example if you have a question.

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

          @JonB sorry for write whole code. but can you give example to how to deallocate memory for recent mentioned singleton class ?

          jsulmJ 1 Reply Last reply
          0
          • Q Qt embedded developer

            @JonB sorry for write whole code. but can you give example to how to deallocate memory for recent mentioned singleton class ?

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

            @Qt-embedded-developer Can you please explain what the problem is?
            I already gave you an example how to deallocate memory...

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

            Q 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Qt-embedded-developer Can you please explain what the problem is?
              I already gave you an example how to deallocate memory...

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

              @jsulm yes i understood that. but to deallocate memory for below 2 statement i need example.

              std::map<int,sqlite3 *> sqliteHandleMap;

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

              i have tried to implement deallocate memory for them like below function in that class like below :

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

              but it gives me many errors like below:

                      ^
              

              "src\SqliteManagerBGSync.h", line 42: Error: #260-D: explicit type is missing ("int" assumed)
              cleanup();
              ^
              "src\SqliteManagerBGSync.cpp", line 25: Error: #260-D: explicit type is missing ("int" assumed)
              SqliteManagerBGSync::cleanup()
              ^
              "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
              for (const auto& kv : sqliteManagerMap) {
              ^
              "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
              for (const auto& kv : sqliteManagerMap) {
              ^
              "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
              for (const auto& kv : sqliteManagerMap) {
              ^
              "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
              for (const auto& kv : sqliteManagerMap) {
              ^
              "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
              for (const auto& kv : sqliteManagerMap) {
              ^
              "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
              delete kv.second;
              ^
              "src\SqliteManagerBGSync.cpp", line 32: Error: #940-D: missing return statement at end of non-void function "app::dbmanager::SqliteManagerBGSync::cleanup"
              }

              jsulmJ 1 Reply Last reply
              0
              • Q Qt embedded developer

                I have seen that my project code is use singleton class to access database.

                I want to free the memory allocated by this class how to do it ? can any body give example for that ?

                #ifndef SQLMANAGER_H_
                #define SQLMANAGER_H_
                
                #include <boomer.h>
                #include <sqlite/sqlite3.h>
                #include <vector>
                #include <string>
                #include "Header/AFCSTicket.h"
                //#include "Error.h"
                
                using namespace std;
                
                namespace app
                {
                namespace dbmanager
                {
                
                class SqliteManager {
                
                public :
                	static	SqliteManager* GetDBInstance();
                	sqlite3* handle;
                	sqlite3_stmt *statement;
                	static sqlite3* getDataBaseHandle();
                
                	int prepareStatement(const char *query);
                	int openDatabase();
                	void closeDatabase();
                	boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle);
                	boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle);
                	void closeDatabaseForDBBackup();
                private:
                	SqliteManager();
                	static SqliteManager* iSqlInstanceDB;
                
                };
                }
                
                }
                #endif
                
                
                
                #include "SqliteManager.h"
                #include <stdio.h>
                #include <string>
                
                
                using namespace std;
                
                using namespace boomer::error;
                
                const char* DB_PATH_MAIN = "CONFIG.DB";
                
                namespace app
                {
                namespace dbmanager
                {
                SqliteManager* SqliteManager::iSqlInstanceDB = NULL;
                SqliteManager::SqliteManager()
                {
                	handle = NULL;
                }
                
                SqliteManager* SqliteManager::GetDBInstance()
                {
                	DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
                	DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
                
                	if ( iSqlInstanceDB == NULL ) {
                		iSqlInstanceDB = new SqliteManager();
                		DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
                	  DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
                	}
                	else
                	{
                	DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max());
                	DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current());
                	}
                	return iSqlInstanceDB;
                }
                
                int SqliteManager::openDatabase()
                {
                	int memused = sqlite3_memory_used();
                    DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
                
                	if(handle != NULL)
                	{
                		DBGF_TRACE("before SqliteManager::openDatabase _heap_max() = %d",_heap_max());
                		DBGF_TRACE("before SqliteManager::openDatabase _heap_current() = %d",_heap_current());
                
                
                		int val = sqlite3_db_release_memory(handle);
                
                	    memused = sqlite3_memory_used();
                		DBGF_TRACE("AFCS:SqliteManager::closeDatabase()::memused=%d",memused);
                
                		DBGF_TRACE("SqliteManager::openDatabase sqlite3_db_cacheflush val = %d",val);
                
                		DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max());
                		DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current());
                		return SUCCESS;
                	}
                
                	int res = sqlite3_open(DB_PATH_MAIN, &handle);
                	if(res != SQLITE_OK)
                	{
                  	DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max());
                		DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current());
                		ERROR_RETURN(0);
                	}
                	else
                	{
                		DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
                	  DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
                
                	    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("openDatabase _heap_max() = %d",_heap_max());
                		  DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
                			return SUCCESS;
                		}
                		else
                		{
                			DBGF_TRACE("SQLITE QUERY FAIL: PRAGMA journal_mode = WAL;PRAGMA  cache_size = 100000;");
                			DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
                			DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
                
                			ERROR_RETURN(0);
                		}
                		DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max());
                		DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current());
                		return SUCCESS;
                	}
                
                }
                
                int SqliteManager::prepareStatement(const char *query)
                {
                	if(openDatabase() != SUCCESS)
                	{
                		ERROR_RETURN(0);
                	}
                
                	statement = NULL;
                	int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0);
                
                	if(res != SQLITE_OK)
                	{
                		sqlite3_finalize(statement);
                		statement = NULL;
                
                		ERROR_RETURN(0);
                
                	}else{
                		return SUCCESS;
                	}
                }
                
                
                void SqliteManager::closeDatabase()
                {
                	DBGF_TRACE("_heap_max() = %d",_heap_max());
                    DBGF_TRACE("_heap_current() = %d",_heap_current());
                	//	if (handle != NULL)
                	//	{
                	//		if(sqlite3_close(handle) == SQLITE_OK)
                	//		{
                	//			DBGF_TRACE("Close Database Success: %s", sqlite3_errmsg(handle));
                	//			handle = NULL;
                	//		}
                	//		else
                	//			LOGF_ERROR("Close Database Error: %s", sqlite3_errmsg(handle));
                	//	}
                }
                
                boomer::transaction::TransactionResult SqliteManager::connectToDataBase(std::string dbFilename , sqlite3* &handle)
                {
                	int res = 0;
                	res = sqlite3_open(dbFilename.c_str(), &handle);
                	if( res != SQLITE_OK )
                	{
                		ERROR_RETURN(CONFIG_DB_CONNECT_FAILED);
                	}
                
                	return boomer::transaction::trOk;
                }
                
                boomer::transaction::TransactionResult SqliteManager::disconnectDataBase(sqlite3* &handle)
                {
                	long errCode = 0;
                	//Disconnect to DataBase
                	int res = sqlite3_close(handle);
                	if(res)
                	{
                		errCode = CONFIG_DB_DISCONNECT_FAILED;
                		return res;
                	}
                	handle = NULL;
                	return boomer::transaction::trOk;
                }
                void SqliteManager::closeDatabaseForDBBackup()
                {
                	DBGF_TRACE("AFCS:SqliteManager::closeDatabaseForDBBackup()");
                	if (handle != NULL)
                	{
                		if(sqlite3_close(handle) == SQLITE_OK)
                		{
                			DBGF_TRACE("closeDatabaseForDBBackup::Close Database Success: %s", sqlite3_errmsg(handle));
                			handle = NULL;
                		}
                		else
                		{
                			//LOGF_ERROR("closeDatabaseForDBBackup::Close Database Error: %s", sqlite3_errmsg(handle));
                		}
                	}
                }
                
                }
                
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #8

                @Qt-embedded-developer deleting a singleton instance is not a trivial matter, you have to follow proper guidelines, or you program may explode.

                I would suggest reading and following
                https://www.interviewsansar.com/singleton-class-delete-design/


                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

                  @jsulm yes i understood that. but to deallocate memory for below 2 statement i need example.

                  std::map<int,sqlite3 *> sqliteHandleMap;

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

                  i have tried to implement deallocate memory for them like below function in that class like below :

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

                  but it gives me many errors like below:

                          ^
                  

                  "src\SqliteManagerBGSync.h", line 42: Error: #260-D: explicit type is missing ("int" assumed)
                  cleanup();
                  ^
                  "src\SqliteManagerBGSync.cpp", line 25: Error: #260-D: explicit type is missing ("int" assumed)
                  SqliteManagerBGSync::cleanup()
                  ^
                  "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                  for (const auto& kv : sqliteManagerMap) {
                  ^
                  "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                  for (const auto& kv : sqliteManagerMap) {
                  ^
                  "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                  for (const auto& kv : sqliteManagerMap) {
                  ^
                  "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                  for (const auto& kv : sqliteManagerMap) {
                  ^
                  "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                  for (const auto& kv : sqliteManagerMap) {
                  ^
                  "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                  delete kv.second;
                  ^
                  "src\SqliteManagerBGSync.cpp", line 32: Error: #940-D: missing return statement at end of non-void function "app::dbmanager::SqliteManagerBGSync::cleanup"
                  }

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

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

                  SqliteManagerBGSync::cleanup()

                  C++ basics: return type is missing...

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

                  Q 1 Reply Last reply
                  1
                  • jsulmJ jsulm

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

                    SqliteManagerBGSync::cleanup()

                    C++ basics: return type is missing...

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

                    @jsulm hi jsulm i am returning nothing. my return type is void.

                    jsulmJ 1 Reply Last reply
                    0
                    • Q Qt embedded developer

                      @jsulm hi jsulm i am returning nothing. my return type is void.

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

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

                      my return type is void.

                      Then write void there. Come on this is really basic stuff...

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

                      Q 1 Reply Last reply
                      1
                      • jsulmJ jsulm

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

                        my return type is void.

                        Then write void there. Come on this is really basic stuff...

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

                        @jsulm Okay thanks. but still i am getting below errors:

                        "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                        for (const auto& kv : sqliteManagerMap) {
                        ^
                        "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                        for (const auto& kv : sqliteManagerMap) {
                        ^
                        "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                        for (const auto& kv : sqliteManagerMap) {
                        ^
                        "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                        for (const auto& kv : sqliteManagerMap) {
                        ^
                        "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                        for (const auto& kv : sqliteManagerMap) {
                        ^
                        "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                        delete kv.second;

                        KroMignonK J.HilkJ 2 Replies Last reply
                        0
                        • Q Qt embedded developer

                          @jsulm Okay thanks. but still i am getting below errors:

                          "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                          for (const auto& kv : sqliteManagerMap) {
                          ^
                          "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                          for (const auto& kv : sqliteManagerMap) {
                          ^
                          "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                          for (const auto& kv : sqliteManagerMap) {
                          ^
                          "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                          for (const auto& kv : sqliteManagerMap) {
                          ^
                          "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                          for (const auto& kv : sqliteManagerMap) {
                          ^
                          "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                          delete kv.second;

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

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

                          Okay thanks. but still i am getting below errors:

                          Do your compiler support C++11 and did you enable C++11 support (eg. add CONFIG += c++11 in your PRO file)?

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

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

                            Okay thanks. but still i am getting below errors:

                            Do your compiler support C++11 and did you enable C++11 support (eg. add CONFIG += c++11 in your PRO file)?

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

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

                            KroMignonK 1 Reply Last reply
                            0
                            • Q Qt embedded developer

                              @jsulm Okay thanks. but still i am getting below errors:

                              "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                              for (const auto& kv : sqliteManagerMap) {
                              ^
                              "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                              for (const auto& kv : sqliteManagerMap) {
                              ^
                              "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                              for (const auto& kv : sqliteManagerMap) {
                              ^
                              "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                              for (const auto& kv : sqliteManagerMap) {
                              ^
                              "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                              for (const auto& kv : sqliteManagerMap) {
                              ^
                              "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                              delete kv.second;

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #15

                              @Qt-embedded-developer

                              SqliteManagerBGSync();
                              cleanup();
                              

                              both are missing the return type, and also make sure to fix them in the *.cpp file as well!


                              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
                              0
                              • J.HilkJ J.Hilk

                                @Qt-embedded-developer

                                SqliteManagerBGSync();
                                cleanup();
                                

                                both are missing the return type, and also make sure to fix them in the *.cpp file as well!

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

                                @J-Hilk i have changed it . i have used void, But still i am getting as below mentioned error:

                                "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                                for (const auto& kv : sqliteManagerMap) {
                                ^
                                "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                                for (const auto& kv : sqliteManagerMap) {
                                ^
                                "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                                for (const auto& kv : sqliteManagerMap) {
                                ^
                                "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                                for (const auto& kv : sqliteManagerMap) {
                                ^
                                "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                                for (const auto& kv : sqliteManagerMap) {
                                ^
                                "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                                delete kv.second;

                                jsulmJ 1 Reply Last reply
                                0
                                • Q Qt embedded developer

                                  @J-Hilk i have changed it . i have used void, But still i am getting as below mentioned error:

                                  "src\SqliteManagerBGSync.cpp", line 27: Error: #260-D: explicit type is missing ("int" assumed)
                                  for (const auto& kv : sqliteManagerMap) {
                                  ^
                                  "src\SqliteManagerBGSync.cpp", line 27: Error: #252: reference variable "kv" requires an initializer
                                  for (const auto& kv : sqliteManagerMap) {
                                  ^
                                  "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                                  for (const auto& kv : sqliteManagerMap) {
                                  ^
                                  "src\SqliteManagerBGSync.cpp", line 27: Error: #29: expected an expression
                                  for (const auto& kv : sqliteManagerMap) {
                                  ^
                                  "src\SqliteManagerBGSync.cpp", line 27: Error: #65: expected a ";"
                                  for (const auto& kv : sqliteManagerMap) {
                                  ^
                                  "src\SqliteManagerBGSync.cpp", line 28: Error: #153: expression must have class type
                                  delete kv.second;

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

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

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

                                  Q 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @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 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();
                                    
                                    }
                                    
                                    jsulmJ 1 Reply Last reply
                                    0
                                    • Q Qt embedded developer

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

                                      KroMignonK Offline
                                      KroMignonK Offline
                                      KroMignon
                                      wrote on 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
                                      3
                                      • Q Qt embedded developer

                                        @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();
                                        
                                        }
                                        
                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 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
                                        • KroMignonK KroMignon

                                          @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 last edited by
                                          #21

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

                                          J.HilkJ KroMignonK 2 Replies 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