简体   繁体   English

在MySQL中使用getopt lib时可疑的内存泄漏?

[英]Suspicious memory leak when using getopt lib in MySQL?

内存使用率和线程使用率监视器

I am posting this post on behalf of my college. 我代表我的大学张贴这篇文章。

He found a suspicious memory leak when using the handle_option (MySQL getopt lib) to read config file (/etc/my.cnf) 当使用handle_option(MySQL getopt lib)读取配置文件(/etc/my.cnf)时,他发现了可疑的内存泄漏。

He execute the Source code below after malloc host_name, user name: 他在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);

He mention about the method above is using Error(Segment) instead of using free(host_name) and free(user_name)? 他提到上述方法是使用Error(Segment)而不是使用free(host_name)和free(user_name)吗? So this is the possible reason of causing memory leakage? 那么这是导致内存泄漏的可能原因吗?

Well.. I have zero basic on MySQL, so I might not able to delivery 100% of the problem description. 好吧..我对MySQL的基础知识为零,所以我可能无法交付100%的问题描述。 So, feel free to query on this and I will update the details of problem descriptions as per the query. 因此,随时对此进行查询,我将根据查询更新问题描述的详细信息。

My college is having language barrier so I am posting on behalf of him. 我的大学有语言障碍,所以我代表他发布。

Valgrind Report: 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)

LEAK SUMMARY: 泄漏摘要:

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)

Put printf("user_name: %p; host_name: %p\\n", (void *) user_name, (void *) host_name); printf("user_name: %p; host_name: %p\\n", (void *) user_name, (void *) host_name); before and after the call to handle_options and run your code. 调用handle_options之前和之后并运行您的代码。 Do the additional two lines printed as a result differ from each other? 结果打印的另外两行是否彼此不同? If so, your diagnosis is correct and user_name and host_name are changed by handle_options, and perhaps using a malloc'd pointer is unsuitable for this function. 如果是这样,则您的诊断是正确的,并且handle_options更改了user_name和host_name,并且可能不适合使用使用malloc指针的函数。

If not, your diagnosis is incorrect and the memory leak lies elsewhere. 否则,您的诊断不正确,并且内存泄漏在其他地方。 You'd want to look at the source code for MS_MYSQL_init, main_proc and main, in that order, which are the three functions listed by valgrind from your project. 您需要按顺序查看MS_MYSQL_init,main_proc和main的源代码,这是valgrind从您的项目中列出的三个函数。 Let me know if you need my help... 让我知道您是否需要我的帮助...

I'd say the combination of allocating memory to the pointer my_option.value is pointing to along with the use of GET_STR is leading to leaking what had been allocated to my_option.value , as GET_PTR initalises what my_option.value is pointing to, to point right to somewhere into what had been passed as argv to handle_options , without freeing what the value my_option.value is pointing, previously pointed to. 我想说分配内存指针的组合my_option.value指向与使用沿着GET_STR是导致漏水什么已分配给my_option.value ,作为GET_PTR initalises什么my_option.value指向,指向有权移至以argv传递到handle_options ,而无需释放先前指向的my_option.value所指向的值。

To get around this, either do not allocate any memory to what my_option.value is pointing to prior to passing it to handle_options or allocate it using my_alloc() and use GET_PTR_ALLOC as value type, as GET_PTR_ALLOC implies a call to my_free() on what my_option.value is pointing to prior to re-initialising what it is pointing to. 为了解决这个问题,要么不分配任何内存什么my_option.value将其传递给指向之前handle_options或使用分配它my_alloc()并使用GET_PTR_ALLOC为价值型,为GET_PTR_ALLOC意味着调用my_free()什么my_option.value在重新初始化它指向之前指向。


Just out of curiosity: What is uchar , and why do you cast to uchar ** and not to void * , the type of my_option.value ? 出于好奇:什么是uchar ,为什么要转换为my_option.value类型的uchar **而不是void *


Also this 还有这个

"user", "u", "userID", "h",(uchar**) & user_name ...

should read 应该读

"user", "u", "userID", (uchar**) & user_name ...

shouldn't it? 不是吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM