C code connection to MySQL database memory leak problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.