mysql连接数据库函数内存泄露解决方案
2011-08-25 23:07
274 查看
转自:http://blog.csdn.net/weihua1984/archive/2010/08/24/5835731.aspx
linux 下使用c操作mysql数据库时写个测试程序
view plaincopy to clipboardprint?
int main(void)
{
MYSQL m_mysql;
mysql_init(&m_mysql);
const char pLocalhost[] = "localhost";
const char pUser[] = "root";
const char pPass[] = "123456";
const char pDB[] = "HTTPCONTROL";
if(mysql_real_connect(&m_mysql,pLocalhost,pUser,pPass,pDB,0,NULL,0) == NULL)
//mysql_connect() 连接一个MySQL服务器。该函数不推荐;使用
cout<<"connect failed"<<endl;
else
cout<<"connect success"<<endl;
mysql_close(&m_mysql);
return 0;
}
编译后使用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(。。。。)
linux 下使用c操作mysql数据库时写个测试程序
view plaincopy to clipboardprint?
int main(void)
{
MYSQL m_mysql;
mysql_init(&m_mysql);
const char pLocalhost[] = "localhost";
const char pUser[] = "root";
const char pPass[] = "123456";
const char pDB[] = "HTTPCONTROL";
if(mysql_real_connect(&m_mysql,pLocalhost,pUser,pPass,pDB,0,NULL,0) == NULL)
//mysql_connect() 连接一个MySQL服务器。该函数不推荐;使用
mysql_real_connect()代替。
cout<<"connect failed"<<endl;
else
cout<<"connect success"<<endl;
mysql_close(&m_mysql);
return 0;
}
编译后使用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(。。。。)
相关文章推荐
- php mysql_connect 连接数据库失败解决方案
- 远程连接linux的mysql服务报错10061的解决方案
- win7下 使用MFC 连接mysql 引用ado2 的解决方案
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- MySQL无法同时多个客户端进行连接解决方案
- Navicat MySQL连接Linux下MySQL的问题解决方案
- c3p0连接mysql经常断开连接解决方案
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- centos中mysql远程连接失败解决方案
- 远程mysql服务器无法连接解决方案
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- mysql“因客户端异常退出导致下次无法连接”的解决方案
- 关于MySQL的wait_timeout连接超时问题报错解决方案
- mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 详细出处参考:http://www.jb51.net/article/32284.htm
- ubuntu安装mysql后无法远程连接解决方案
- python3 连接 mysql 遇到乱码问题解决方案
- 连接mysql发生错误1130的解决方案
- centos中mysql远程连接失败解决方案
- C#连接MySql中文乱码的解决方案
- MySQL远程连接Host 'XXX' is not allowed to connect to this MySQL server 解决方案