I have the following code structure (The whole code is huge and I think this snippet is relevant to my problem),
MYSQL_RES *res_set;
MYSQL_ROW row;
MYSQL *connect;
int main()
{
connect=mysql_init(NULL);
mysql_real_connect(connect, NULL, "root", "suvp" ,"Employees" ,0,NULL,0);
/*Other Code*/
mysql_free_result(res_set);
mysql_close(connect);
}
The variables for database connection are global. In my "Other Code" I make call to other functions which uses initialised connect
(in mysql_query
etc )and row
and res_set
when necessary. and as seen I free the result and close the connection at the end of main.
I reuse res_set
(without freeing it each time) from one function to other. Will that cause an issue?
In all the functions that I use, the staments are similar
mysql_query(connect,myQuery.c_str())
res_set = mysql_store_result(connect);
row = mysql_fetch_row(res_set);
Valgrind reports this,
==4864== LEAK SUMMARY:
==4864== definitely lost: 0 bytes in 0 blocks
==4864== indirectly lost: 0 bytes in 0 blocks
==4864== possibly lost: 0 bytes in 0 blocks
==4864== still reachable: 99,954 bytes in 30 blocks
==4864== suppressed: 0 bytes in 0 blocks
Detailed errors point out to
mysql_real_connect
called from my main function.
As per this page calling mysql_library_end()
is good practise. But then even after I call mysql_library_end()
after closing my connection. Valgrind Says,
==5120== HEAP SUMMARY:
==5120== in use at exit: 116,466 bytes in 34 blocks
==5120== total heap usage: 95 allocs, 61 frees, 147,218 bytes allocated
==5120== LEAK SUMMARY:
==5120== definitely lost: 0 bytes in 0 blocks
==5120== indirectly lost: 0 bytes in 0 blocks
==5120== possibly lost: 0 bytes in 0 blocks
==5120== still reachable: 116,466 bytes in 34 blocks
==5120== suppressed: 0 bytes in 0 blocks
And as earlier, they all boil down to mysql_real_connect
The program runs fine. But there are problems indicated by valgrind. Where am I going wrong?