Linux/C++: getting user's directory without leaks -
what doing wrongly there memory leaks in following code tries read home directory of user?
static std::string gethomedir() { struct passwd *pw = getpwuid(getuid()); std::string res( pw->pw_dir); endpwent(); return res; }
valgrind complains:
==32757== 160 (40 direct, 120 indirect) bytes in 1 blocks lost in loss record 42 of 48 ==32757== @ 0x402bb7a: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==32757== 0x456e84e: nss_parse_service_list (nsswitch.c:678) ==32757== 0x456efc9: __nss_database_lookup (nsswitch.c:175) ==32757== 0x4a8e168: ??? ==32757== 0x4a8fb5c: ??? ==32757== 0x4525fa6: getpwuid_r@@glibc_2.1.2 (getxxbyyy_r.c:256) ==32757== 0x45258ed: getpwuid (getxxbyyy.c:117) ==32757== 0x805ad56: gethomedir() (configreader.cpp:73)
(also, sidenote: man getpwuid
show example program, leaks same amount of memory...)
(and want compare linuxes:
uname -a
gives linux reference 3.5.0-47-generic #71-ubuntu smp tue feb 18 23:59:30 utc 2014 i686 athlon i686 gnu/linux
... ubuntu 12.10
this known bug:
nss_parse_service_list leaks tiny amount of memory
it considered resolved "wontfix", comment stating "the memory allocated once hold global state, , isn't freed."
Comments
Post a Comment