Can any body explain me below statement of function ?
-
@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 ?
-
@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 ?
@Qt-embedded-developer said in Can any body explain me below statement of function ?:
static function defination
A "static function defination" simply means the function/method exists outside any instance of a class. It has no implication for storage.
std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
(nostatic
) would be just a local variable in the function, destroyed after the function call completes.If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-
static
) member variable in the class. But then different instances of the class have separate maps, they do not share a single map. -
@Qt-embedded-developer said in Can any body explain me below statement of function ?:
static function defination
A "static function defination" simply means the function/method exists outside any instance of a class. It has no implication for storage.
std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
(nostatic
) would be just a local variable in the function, destroyed after the function call completes.If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-
static
) member variable in the class. But then different instances of the class have separate maps, they do not share a single map.@JonB said in Can any body explain me below statement of function ?:
declare it as a (non-static) member variable in the class
i think above sentence is incorrect. it may be static instead non-static.
-
@JonB said in Can any body explain me below statement of function ?:
declare it as a (non-static) member variable in the class
i think above sentence is incorrect. it may be static instead non-static.
@Qt-embedded-developer said in Can any body explain me below statement of function ?:
i think above sentence is incorrect.
Pardon? Member variables might be
static
or not. With different lifetime correspondingly. You asked aboutstatic std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
"when this map not declared as static ", which I answered. I wrote:If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class
which is correct. May or may not be what you want, but it's correct....
-
@Qt-embedded-developer said in Can any body explain me below statement of function ?:
i think above sentence is incorrect.
Pardon? Member variables might be
static
or not. With different lifetime correspondingly. You asked aboutstatic std::map<int, SqliteManagerBGSync*> sqliteManagerMap;
"when this map not declared as static ", which I answered. I wrote:If you want the map to persist throughout the lifetime of each instance of a class, declare it as a (non-static) member variable in the class
which is correct. May or may not be what you want, but it's correct....
@JonB Thanks
-
@Qt-embedded-developer said in Can any body explain me below statement of function ?:
i want to know the what is technical meaning of below statement
sqliteManagerMap.insert(std::pair<int,SqliteManagerBGSync>(threadId,new SqliteManagerBGSync()));
shall i need to free memory allocated to SqliteManagerBGSync() ?I think the idea between this is to get access to DB connection which has been created for the current thread (as QSqlDatabase has to be used in the thread in which it has been created).
But to answer to your question, yes you have to free used memory.
The easiest way to do it, would be to use smart pointer, likeQSharedPointer
==> useQSharedPointer<SqliteManagerBGSync>
instead ofSqliteManagerBGSync*
.@KroMignon thanks for suggesting smart pointer.