Always use C code to connect MySQL database, today with valgrind detection, found that there is a memory leak problem
The code is as follows
mysql*== mysql_real_connect (connection, ...); The mysql_query (...); Mysql_close (connection);
On-line search, find the following solutions
http://pipal.iteye.com/blog/903506
Add Mysql_library_end () after Mysal_close ().
Test, found still leaked.
Helpless after the brother, plus query MySQL official website document, found that Mysql_real_connect () has the following mystery:
Return Values
A MYSQL*
Connection Handle If the connection is successful, NULL
if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.
If the Mysql_real_connect () connection succeeds, the pointer returns the same as the first parameter, and if it fails, returns null
Inside my Code, connection first points to Mysql_init () and then to the return pointer of Mysql_real_connect (), and if it succeeds, the two pointers are the same, however, on my machine, the connection fails, So Mysql_init () The returned pointer is missing, the subsequent close I release is a null pointer, the missing pointer is not released, so the memory leaks
This is a non-mainstream error.
This thing warns me never to use a variable for a variety of purposes, especially pointers, pointers to a region, do not change, otherwise, it is easy to leak memory, which is the reason for the introduction of C + + reference.
C code connection to MySQL database memory leak problem