mysql - Valgrind 可能丢失 - MYSQL

标签 mysql c memory-leaks valgrind

我最近一直在通过 Valgrind 运行我的一个应用程序,但有一些与 MYSQL 相关的漏洞我无法修复。我将有问题的代码放在最基本的形式中并进行了测试;我有同样的泄漏。我应该忽略它们还是我做错了什么?

代码:

#include <stdio.h>
#include <stdlib.h>

#include <mysql/mysql.h>

int main()
{
    MYSQL *MYSQLIns;

    MYSQLIns = mysql_init(NULL);

    mysql_real_connect(MYSQLIns, "localhost", "username", "password", "database", 0, NULL, 0);

    mysql_close(MYSQLIns);

    return EXIT_SUCCESS;
}

编译:

gcc -g -lmysqlclient mysql_mem_test.c -o mysql_mem_test

Valgrind 输出:

valgrind --leak-check=full ./mysql_mem_test
==4601== Memcheck, a memory error detector
==4601== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4601== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
==4601== Command: ./mysql_mem_test
==4601== 
==4601== 
==4601== HEAP SUMMARY:
==4601==     in use at exit: 61,336 bytes in 16 blocks
==4601==   total heap usage: 70 allocs, 54 frees, 109,218 bytes allocated
==4601== 
==4601== 24,528 bytes in 6 blocks are possibly lost in loss record 4 of 5
==4601==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==4601==    by 0x4079BB2: my_once_alloc (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407A408: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407AED1: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407B112: get_charset_by_csname (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x409D55B: mysql_init_character_set (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x409F5A1: mysql_real_connect (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x80485E0: main (mysql_mem_test.c:12)
==4601== 
==4601== 28,616 bytes in 7 blocks are possibly lost in loss record 5 of 5
==4601==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==4601==    by 0x4079BB2: my_once_alloc (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407A3E9: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407AED1: ??? (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x407B112: get_charset_by_csname (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x409D55B: mysql_init_character_set (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x409F5A1: mysql_real_connect (in /usr/lib/libmysqlclient.so.16.0.0)
==4601==    by 0x80485E0: main (mysql_mem_test.c:12)
==4601== 
==4601== LEAK SUMMARY:
==4601==    definitely lost: 0 bytes in 0 blocks
==4601==    indirectly lost: 0 bytes in 0 blocks
==4601==      possibly lost: 53,144 bytes in 13 blocks
==4601==    still reachable: 8,192 bytes in 3 blocks
==4601==         suppressed: 0 bytes in 0 blocks
==4601== Reachable blocks (those to which a pointer was found) are not shown.
==4601== To see them, rerun with: --leak-check=full --show-reachable=yes
==4601== 
==4601== For counts of detected and suppressed errors, rerun with: -v
==4601== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 55 from 38)

最佳答案

在 mysql_close 之后添加 mysql_library_end(); 有帮助吗?

更新:安装了 valgrind 并尝试了一下,确实如此。 mysql_init() 隐含地执行 mysql_library_init(),但 mysql_close() 并没有类似地执行 mysql_library_end(),文档暗示了这一点,但没有直接说出来。

关于mysql - Valgrind 可能丢失 - MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3597049/

相关文章:

c - 如何让Ctrl-c结束程序

arrays - 将字符串 char 设置为 null 会导致 C 中的内存泄漏吗?

php - 需要帮助将调查响应从 HTML 表单传递到 MySQL 表

mysql - 使用MySql函数变量作为查询中的表名

c - 这个 << 语句是什么意思?

c - 检测 TCP 中的其他对等方已关闭

java - OkHttp:避免泄漏连接警告

iphone - 弹出 Controller 时 UINavigationController 内存不会减少

mysql - 存储过程挂起

javascript - PHP/mysql - 如何将变量从类传递到单独的 index.php?个人资料页面?