linux mysql mysql_real_connect函数导致内存泄露

自己写个了个小东西,Linux下C语言,数据库使用的mysql,用valgrind调试的时候一直提示内存泄露,问题都没找到,后来搜索了一下,最后发现是mysql_real_connect惹的祸。

提示的错误:

==11867== 24,528 bytes in 6 blocks are possibly lost in loss record 4 of 5
==11867==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==11867==    by 0x408F53D: my_once_alloc (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x408FDB8: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x4090889: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x4090ADB: get_charset_by_csname (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x40B3211: mysql_init_character_set (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x40B52DB: mysql_real_connect (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x804DCBE: testDbState (database.c:19)
==11867==    by 0x8049811: main (main.c:5)
==11867== 
==11867== 28,616 bytes in 7 blocks are possibly lost in loss record 5 of 5
==11867==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==11867==    by 0x408F53D: my_once_alloc (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x408FD99: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x4090889: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x4090ADB: get_charset_by_csname (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x40B3211: mysql_init_character_set (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x40B52DB: mysql_real_connect (in /usr/lib/libmysqlclient.so.16.0.0)
==11867==    by 0x804DCBE: testDbState (database.c:19)
==11867==    by 0x8049811: main (main.c:5)
==11867== 
==11867== LEAK SUMMARY:
==11867==    definitely lost: 0 bytes in 0 blocks
==11867==    indirectly lost: 0 bytes in 0 blocks
==11867==      possibly lost: 53,144 bytes in 13 blocks
==11867==    still reachable: 8,192 bytes in 3 blocks
==11867==         suppressed: 0 bytes in 0 blocks
==11867== 
==11867== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 37 from 12)
--11867-- 
--11867-- used_suppression:     37 dl-hack3-cond-1
==11867== 
==11867== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 37 from 12)

 

 

原来使用mysql_real_connect之后,断开连接后必须得mysql_library_end();

  mysql_close(conn);
  conn=NULL;
  mysql_library_end();

这样再次调试就不出错了,看到这句话

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 37 from 12)
我激动啊。

valgrind --db-attach=yes --leak-check=full --show-reachable=yes  --tool=memcheck -v ./main

 

 

--10599-- REDIR: 0x42ac880 (strnlen) redirected to 0x4026c50 (strnlen)
--10599-- Discarding syms at 0x4836a20-0x483de08 in /lib/libnss_files-2.12.1.so due to munmap()
--10599-- Discarding syms at 0x402c8b0-0x402d6c8 in /lib/libnss_mdns4_minimal.so.2 due to munmap()
--10599-- Discarding syms at 0x4030bb0-0x4033bd8 in /lib/libnss_dns-2.12.1.so due to munmap()
--10599-- Discarding syms at 0x5458630-0x54646d8 in /lib/libresolv-2.12.1.so due to munmap()
==10599== 
==10599== HEAP SUMMARY:
==10599==     in use at exit: 0 bytes in 0 blocks
==10599==   total heap usage: 3,255 allocs, 3,255 frees, 82,930,848 bytes allocated
==10599== 
==10599== All heap blocks were freed -- no leaks are possible
==10599== 
==10599== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 37 from 12)
--10599-- 
--10599-- used_suppression:     37 dl-hack3-cond-1
==10599== 
==10599== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 37 from 12)

  

 

 

 参考资料:http://dev.mysql.com/doc/refman/5.0/en/mysql-library-end.html

                http://blog.csdn.net/weihua1984/archive/2010/08/24/5835731.aspx

 

你可能感兴趣的:(Linux下MySql)