MySQL串連資料庫函數記憶體泄露解決方案

來源:互聯網
上載者:User

Linux 下使用C操作MySQL資料庫時寫個測試程式

  1. int main(void)  
  2. {  
  3.     MYSQL m_mysql;  
  4.     mysql_init(&m_mysql);  
  5.       
  6.     const char pLocalhost[] = "localhost";  
  7.     const char pUser[] = "root";  
  8.     const char pPass[] = "123456";  
  9.     const char pDB[] = "HTTPCONTROL";  
  10.       
  11.     if(mysql_real_connect(&m_mysql,pLocalhost,pUser,pPass,pDB,0,NULL,0) == NULL) 
  12. //mysql_connect() 串連一個MySQL伺服器。該函數不推薦;使用mysql_real_connect()代替。
  13.         cout<<"connect failed"<<endl;  
  14.     else  
  15.         cout<<"connect success"<<endl;  
  16.        
  17.     mysql_close(&m_mysql);      
  18.     return 0;  
  19. }  

編譯後使用valgrind檢測記憶體泄露

[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、解決措施

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

簡單的說是在斷開資料庫連接後使用

mysql_library_end()

關閉MYSQL 使用的庫即在mysql_close(。。。)後添加mysql_library_end()就ok了

再添加一句,在使用mysql API之前最好是調用mysql_library_init(。。。。)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.