SA-MP Forums Archive
[Confused(sqlite)]What will occur if does not use db_free_result? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [Confused(sqlite)]What will occur if does not use db_free_result? (/showthread.php?tid=174222)



[Unsolved][SQLite]What will occur if does not use db_free_result? - yezizhu - 05.09.2010

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.


Re: [Confused(sqlite)]What will occur if does not use db_free_result? - yezizhu - 06.09.2010

Sry for bump* it.


Re: [Unsolved][SQLite]What will occur if does not use db_free_result? - RoBo - 06.09.2010

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?


Re: [Confused(sqlite)]What will occur if does not use db_free_result? - yezizhu - 06.09.2010

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.