MySQL连接数据库函数内存泄露解决方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL连接数据库函数内存泄露解决方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3234字,纯文字阅读大概需要5分钟。
内容图文
![MySQL连接数据库函数内存泄露解决方案](/upload/InfoBanner/zyjiaocheng/552/9ae21aaf17924e17914557c5cca8f31e.jpg)
Linux 下使用C操作MySQL数据库时写个测试程序 int main(void) { MYSQL m_mysql; mysql_init(m_mysql);
Linux 下使用C操作MySQL数据库时写个测试程序
编译后使用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、解决措施
简单的说是在断开数据库连接后使用
mysql_library_end()
关闭MYSQL 使用的库即在mysql_close(。。。)后添加mysql_library_end()就ok了
再添加一句,在使用mysql API之前最好是调用mysql_library_init(。。。。)
内容总结
以上是互联网集市为您收集整理的MySQL连接数据库函数内存泄露解决方案全部内容,希望文章能够帮你解决MySQL连接数据库函数内存泄露解决方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。