Can any body explain me below statement of function ?
-
@jsulm @KroMignon Can anybody explain me how to free this map and memory allocated to instance ?
@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.
-
@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.
@KroMignon can you give example for my map. how to delete & deallocate memory to it ?
-
@KroMignon can you give example for my map. how to delete & deallocate memory to it ?
@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...
-
@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...
@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 ?
-
@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 ?
@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. -
@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.@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" ?
-
@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" ?
@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.
-
@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" ?
@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 classapp::dbmanager::SqliteManagerBGSync
.
To be totally clean, I would add a static methodcleanup()
to this class to clear properly the map:for (const auto& kv : sqliteManagerMap) { delete kv.second; } sqliteManagerMap.clear();
-
@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.
This post is deleted! -
@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 classapp::dbmanager::SqliteManagerBGSync
.
To be totally clean, I would add a static methodcleanup()
to this class to clear properly the map:for (const auto& kv : sqliteManagerMap) { delete kv.second; } sqliteManagerMap.clear();
@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 .
-
@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 .
@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...
-
@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 .
@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 methodGetDBInstance()
is declared in header as follow:class SqliteManagerBGSync { public: static SqliteManagerBGSync* GetDBInstance(); };
Correct or wrong?
-
@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...
@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.
-
@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 methodGetDBInstance()
is declared in header as follow:class SqliteManagerBGSync { public: static SqliteManagerBGSync* GetDBInstance(); };
Correct or wrong?
@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.
-
@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.
@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 rightSqliteManagerBGSync
for the current thread and create a newSqliteManagerBGSync
if there is no one already created for current thread. -
@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 rightSqliteManagerBGSync
for the current thread and create a newSqliteManagerBGSync
if there is no one already created for current thread.@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 ?
-
@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 ?
@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. -
@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.@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.
-
@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.
@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; };
-
@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; };
@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 ?