我有以下代码,由于某种原因 valgrind 在 getlogin() 函数中发现了一些内存泄漏。
代码:
#include<unistd.h>
#include<stdio.h>
int main()
{
char *userName = getlogin();
printf("%s\n",userName);
return 0;
}
我使用的 valgrind 命令:
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)
为什么会发生这种情况?我该怎么做才能释放它?还有其他方法可以获取用户名吗?提前致谢。
使用 valgrind 3.10.0 我没有错误
==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()
但你的程序没有错误,这种类型的内存泄漏是可以接受的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)