我代表我的学院发布这篇文章。
使用handle_option(MySQL getopt lib)读取配置文件时发现了可疑的内存泄漏(/etc/my.cnf)
他在malloc host_name,用户名:
之后执行下面的源代码char* host_name;
char* user_name;
struct my_option mysql_confgs[] =
{
{"host", "h", "MySQL Server", (uchar**) & host_name, NULL, NULL, GET_STR,
REQUIRED_ARG, 0,0,0,0,0,0},
{"user", "u", "userID", "h",(uchar**) & user_name, NULL, NULL, GET_STR,
REQUIRED_ARG, 0,0,0,0,0,0}
}
handle_options(&argc, &argv, mysql_configs, aux_config_reader);
他提到上面的方法是使用Error(Segment)而不是使用free(host_name)和free(user_name)?那么这是导致内存泄漏的可能原因吗?
嗯..我在MySQL上基本没有,所以我可能无法提供100%的问题描述。因此,请随意查询,我将根据查询更新问题描述的详细信息。
我的大学语言障碍,所以我代表他发帖。
Valgrind报告:
480 bytes in 1 blocks are possibly lost in loss record 26 of 43
at 0x4A068FE: malloc (vg_replace_malloc.c:270)
by 0x33E4E293C1: my_malloc (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2C974: alloc_root (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2E620: ??? (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2F838: my_load_defaults (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x408BF1: MS_MYSQL_init (MS_MYSQL_O.h:109)
by 0x438A39: main_proc (AccLab.c:221)
by 0x437F8A: main (AccLab.c:67)
75,840 bytes in 158 blocks are definitely lost in loss record 41 of 43
at 0x4A068FE: malloc (vg_replace_malloc.c:270)
by 0x33E4E293C1: my_malloc (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2C974: alloc_root (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2E620: ??? (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x33E4E2F838: my_load_defaults (in /usr/lib64/mysql/libmysqlclient.so.16.0.0)
by 0x408BF1: MS_MYSQL_init (MS_MYSQL_O.h:109)
by 0x438A39: main_proc (AccLab.c:221)
by 0x437F8A: main (AccLab.c:67)
泄漏概要:
definitely lost: 75,840 bytes in 158 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 2,304 bytes in 7 blocks
still reachable: 9,675,408 bytes in 2,387 blocks
suppressed: 0 bytes in 0 blocks
Reachable blocks (those to which a pointer was found) are not shown.
To see them, rerun with: --leak-check=full --show-reachable=yes
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 4 from 4)
答案 0 :(得分:0)
在调用handle_options之前和之后放置printf("user_name: %p; host_name: %p\n", (void *) user_name, (void *) host_name);
并运行代码。因此打印的另外两行是否彼此不同?如果是这样,您的诊断是正确的,并且handle_options更改了user_name和host_name,并且可能使用malloc指针不适合此函数。
如果没有,您的诊断不正确,内存泄漏位于其他地方。您希望按顺序查看MS_MYSQL_init,main_proc和main的源代码,这些是valgrind从您的项目中列出的三个函数。如果您需要我的帮助,请告诉我......
答案 1 :(得分:0)
我会说将内存分配给指针my_option.value
的组合指向同时使用GET_STR
会导致泄漏已分配给my_option.value
的内容,因为{ {1}} GET_PTR
指向的内容,指向my_option.value
到argv
所传递的内容的某处,而不释放值handle_options
所指向的内容,先前指出。
要解决这个问题,要么在将my_option.value
传递给my_option.value
或使用handle_options
进行分配并使用my_alloc()
之前,不要将任何内存分配给GET_PTR_ALLOC
指向的内容作为值类型,GET_PTR_ALLOC
意味着在重新初始化指向的内容之前调用my_free()
my_option.value
指向的内容。
出于好奇:什么是uchar
,为什么要投放到uchar **
而不是void *
my_option.value
的类型?
也是这个
"user", "u", "userID", "h",(uchar**) & user_name ...
应该阅读
"user", "u", "userID", (uchar**) & user_name ...
不应该吗?