MySQL database connection Function Memory leakage solution

Source: Internet
Author: User
Tags valgrind

Write a test program when using C in Linux to operate MySQL database

  1. IntMain (Void)
  2. {
  3. MYSQL m_mysql;
  4. Mysql_init (& m_mysql );
  5. Const CharPLocalhost [] ="Localhost";
  6. Const CharPUser [] ="Root";
  7. Const CharPPass [] ="123456";
  8. Const CharPDB [] ="HTTPCONTROL";
  9. If(Mysql_real_connect (& m_mysql, pLocalhost, pUser, pPass, pDB, 0, NULL, 0) = NULL)
  10. // Mysql_connect () connects to a MySQL server. This function is not recommended.mysql_real_connect().
  11. Cout <"Connect failed"<Endl;
  12. Else
  13. Cout <"Connect success"<Endl;
  14. Mysql_close (& m_mysql );
  15. Return0;
  16. }

Use valgrind to detect memory leakage after compilation

[Root @ localhost string] # valgrind -- tool = memcheck -- leak-check = full./mysql
==10352 = Memcheck, a memory error detector
= 10352 = Copyright (C) 2002-2009, and gnu gpl 'd, by Julian Seward et al.
= 10352 = Using Valgrind-3.5.0 and LibVEX; rerun with-h for copyright info
==10352 = Command:./mysql
= 10352 =
Connect success
= 10352 =
==10352 === heap summary:
= 10352 = in use at exit: 61,336 bytes in 16 blocks
= 10352 = total heap usage: 118 allocs, 102 frees, 115,929 bytes allocated
= 10352 =
= 10352 = 24,528 bytes in 6 blocks are possibly lost in loss record 4 of 5
= 10352 = at 0x4005903: malloc (vg_replace_malloc.c: 195)
= 10352 = by 0x30DD75: my_once_alloc (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30E59C :??? (In/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30EF96 :??? (In/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30F111: get_charset_by_csname (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x32C3D2: mysql_init_character_set (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x32DABB: mysql_real_connect (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x8048A76: main (str. cpp: 32)
= 10352 =
= 10352 = 28,616 bytes in 7 blocks are possibly lost in loss record 5 of 5
= 10352 = at 0x4005903: malloc (vg_replace_malloc.c: 195)
= 10352 = by 0x30DD75: my_once_alloc (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30E57A :??? (In/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30EF96 :??? (In/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x30F111: get_charset_by_csname (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x32C3D2: mysql_init_character_set (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x32DABB: mysql_real_connect (in/usr/lib/mysql/libmysqlclient. so.15.0.0)
= 10352 = by 0x8048A76: main (str. cpp: 32)
= 10352 =
= 10352 = leak summary:
= 10352 = definitely lost: 0 bytes in 0 blocks
==10352 = indirectly lost: 0 bytes in 0 blocks
==10352 = possibly lost: 53,144 bytes in 13 blocks
= 10352 = still reachable: 8,192 bytes in 3 blocks
= 10352 = suppressed: 0 bytes in 0 blocks
= 10352 = Reachable blocks (those to which a pointer was found) are not shown.
= 10352 = To see them, rerun with: -- leak-check = full -- show-reachable = yes
= 10352 =
= 10352 = For counts of detected and suppressed errors, rerun with:-v
= 10352 = error summary: 2 errors from 2 contexts (suppressed: 3

2. Solutions

Http://dev.mysql.com/doc/refman/5.0/en/mysql-library-end.html

Simply put, it is used after the database is disconnected.

Mysql_library_end ()

Close the database used by MYSQL in mysql_close (...) And then add mysql_library_end ().

Add another sentence. before using the mysql API, you 'd better call mysql_library_init (....)

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.