Linux的getlogin()的内存泄漏 [英] linux getlogin() memory leak

查看:234
本文介绍了Linux的getlogin()的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,以及由于某种原因Valgrind的发现在getlogin()函数的一些内存泄漏。
在code:

I have the following code, and for some reason valgrind finds some memory leaks in the getlogin() function. The code:

#include<unistd.h>
#include<stdio.h>

int main()
{
    char *userName = getlogin();
    printf("%s\n",userName);

    return 0;
}

该命令Valgrind的,我用:
的valgrind --leak检查=全-v ./

The valgrind command which I use: valgrind --leak-check=full -v ./

错误我得到:

 HEAP SUMMARY:
==2405==     in use at exit: 300 bytes in 11 blocks
==2405==   total heap usage: 67 allocs, 56 frees, 9,106 bytes allocated
==2405== 
==2405== Searching for pointers to 11 not-freed blocks
==2405== Checked 72,264 bytes
==2405== 
==2405== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==2405==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2405==    by 0x4F37CD4: nss_parse_service_list (nsswitch.c:678)
==2405==    by 0x4F38795: __nss_database_lookup (nsswitch.c:175)
==2405==    by 0x55F6623: ???
==2405==    by 0x4EF144C: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==2405==    by 0x4F145AE: __getlogin_r_loginuid (getlogin_r.c:68)
==2405==    by 0x4F14304: getlogin (getlogin.c:35)
==2405==    by 0x400550: main (tmp1.c:6)
==2405== 
==2405== LEAK SUMMARY:
==2405==    definitely lost: 60 bytes in 1 blocks
==2405==    indirectly lost: 240 bytes in 10 blocks
==2405==      possibly lost: 0 bytes in 0 blocks
==2405==    still reachable: 0 bytes in 0 blocks
==2405==         suppressed: 0 bytes in 0 blocks
==2405== 
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--2405-- 
--2405-- used_suppression:      2 dl-hack3-cond-1
==2405== 
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

为什么会发生?而我能做些什么自由呢?是否有任何其他选项,以获取用户名?先谢谢了。

Why does it happen? And what can I do to free it? Is there any other option to get the user name? Thanks in advance.

推荐答案

使用的valgrind 3.10.0我没有误差

With valgrind 3.10.0 I have no error

==27656== 
==27656== HEAP SUMMARY:
==27656==     in use at exit: 0 bytes in 0 blocks
==27656==   total heap usage: 67 allocs, 67 frees, 9,101 bytes allocated
==27656== 
==27656== All heap blocks were freed -- no leaks are possible
==27656== 
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)>

尝试升级或getlogin之后调用__libc_freeres()()
但是,在你的程序没有错误,这种类型的内存泄漏是可以接受的。

Try to upgrade it or call __libc_freeres() after getlogin() But there is no error in your programme, this type of memory leak is acceptable.

这篇关于Linux的getlogin()的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆