21.8.7.25 mysql_free_result()

void mysql_free_result(MYSQL_RES *result)

Description

Frees the memory allocated for a result set by mysql_store_result(), mysql_use_result(), mysql_list_dbs(), and so forth. When you are done with a result set, you must free the memory it uses by calling mysql_free_result().

Do not attempt to access a result set after freeing it.

Return Values

None.

Errors

None.


User Comments
  Posted by mickey on January 19, 2004
mysql_free_result() free the MYSQL_RES, but it didn't set result to NULL. When we want to check result in other code, we can set result as default NULL where after call mysql_free_result(), and Set result as default NULL before store it.

Example:

MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
res = NULL; // Set res as default NULL

mysql_init(&mysql);
mysql_real_connect(&mysql,"localhost","root","","test",0,NULL,0);
mysql_query(&mysql,"SELECT * FROM person WHERE no<\'10\'");
res = mysql_store_result(&mysql);
..... other code here.....
... maybe check res is NULL or not
.......
mysql_free_result(res);
res = NULL; // Set res as default NULL
....
mysql_close(&mysql);

////////////
// Notes:
If result is NULL, we call mysql_free_result(res) is ok.
If call mysql_free_result(res) more once, we can get an memory access error.

Example:(OK)
res = mysql_store_result(&mysql);
......
mysql_free_result(res); // call free result once
res = NULL;
....
mysql_free_result(res); // call free result more then once is ok, because we set res as default NULL when after call mysql_free_result()

Example:(BAD,ERROR)
res = mysql_store_result(&mysql);
......
mysql_free_result(res); // must call free result once
....
mysql_free_result(res); // call free result more then once is BAD, because res is unallocated now.

Sign Up Login You must be logged in to post a comment.