[Confused(sqlite)]What will occur if does not use db_free_result?
#1

I've found some scripters use this code below
Код:
db_free_result(db_query(db_example,"SELECT * FROM example_table"));
Does func"db_free_result" has any effect here?

I think it does no effect, cuz it frees the result which stores in heap(TEMPORARY memory area), but not stack.
Neither it does below.
Код:
new
   DBReuslt:dbr; //stores in heap
   dbr = db_query(db_example,"SELECT * FROM example_table");
   db_free_result(dbr);
Does variable in heap will be cleared when it may not be use(code survival time?)


Or, should we just use db_free_result in this case?
Код:
new
   DBResult:g_dbr; //stores in stack
func dbexe1(){
   g_dbr = db_query(db_example,"example query1");
   //use g_dbr do something here
   db_free_result(g_dbr);
}
func dbexe2(){
   g_dbr = db_query(db_example,"example query2");
   //use g_dbr do something here
   db_free_result(g_dbr);
}
It might be a code optimisation.
Thanks in advanced.
Reply
#2

Sry for bump* it.
Reply
#3

db_query does not actually return the result of the query, it returns a number that points to the query (essentially a pointer). If you delete the pointer to the query then the query will still exist, but there will be no way to access/delete it, keep doing queries and not freeing the result and you'll end up with a memory leak. db_free_result actually clears the result within the server. A little example...

pawn Код:
main()
{
        new object;
        object = CreateObject(...);
        return;
}
That code will assign the objectid to "object", when main has finished executing "object" will be cleared from the heap, but that actual ingame object will still exist, all you will have done it delete the pointer to it.

Understand?
Reply
#4

Thanks both, it seems I can solve some unexplained crash now.


Quote:
Originally Posted by ******
Посмотреть сообщение
Also, both your "dbr" and "object" variables are stored on the stack - which is temporary, not the heap, which is not temporary.
But if attempt to use a very big array in function, it will report "heap size xxxx" when compling, not "stack size xxxx".
So far I cannot clearly understand the differences.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)