getpwuid() 함수 아시는분?
글쓴이: leolo / 작성시간: 금, 2003/06/13 - 5:51오후
getpwuid()도 strdup()와 같이 메모리 누수가 발생하는 함수인가요?
그런거 같은데.. 만약 그렇다면, free는 어떻게 시켜주죠.
dmalloc으로 해보니까.. 메모리 누수가 생기던데요..
제가 테스트를 잘못한건지...
알려주세요..
redhat7.3에서 dmalloc을 사용하였습니다.
#dmalloc -l ./logfile -i 100 high > dmalloc_setup
#source dmalloc_setup
이후 아래의 소스코드를 만들었습니다.
물론 아래내용을 define하고요.
#ifdef DMALLOC
#include <dmalloc.h>
#endif
다음으로 컴파일을 하고 실행화일을 수행하였습니다.
#gcc -g -DDMALLOC test.c -ldmalloc -o test
#test
로그파일 확인
#cat logfile
메모리가 누수가 되더군요.
#include <stdio.h> #include <stdlib.h> #include <grp.h> #include <sys/types.h> #include <pwd.h> #ifdef DMALLOC #include <dmalloc.h> #endif int main() { struct passwd *passwdbuf; if (getuid() == 0) { passwdbuf = (struct passwd *)getpwuid(getuid()); if (passwdbuf == NULL) { printf("getpwuid"); } printf("passwdbuf->pw_name %s", passwdbuf->pw_name); } return 0; }
결과는 root 입니다.
그런데, gcc -g -DDMALLOC test.c -ldmalloc -o test
해서 수행해 본 결과 메모리 누수가 발생하던데요..
logfile 내용...
1055504497: 59: Dmalloc version '4.8.1' from 'http://dmalloc.com/' 1055504497: 59: flags = 0x4f47503, logfile 'logfile' 1055504497: 59: interval = 100, addr = 0, seen # = 0 1055504497: 59: starting time = 1055504497 1055504497: 59: free bucket count/bits: 109/5 35/6 15/8 8/9 3/10 1/11 1055504497: 59: basic-block 4096 bytes, alignment 8 bytes, heap grows up 1055504497: 59: heap: 0x804a000 to 0x8054000, size 40960 bytes (10 blocks) 1055504497: 59: heap checked 0 1055504497: 59: alloc calls: malloc 53, calloc 2, realloc 0, free 4 1055504497: 59: alloc calls: recalloc 0, memalign 0, valloc 0 1055504497: 59: total memory allocated: 3946 bytes (55 pnts) 1055504497: 59: max in use at one time: 3462 bytes (53 pnts) 1055504497: 59: max alloced with 1 call: 1024 bytes 1055504497: 59: max alloc rounding loss: 2906 bytes (45%) 1055504497: 59: max memory space wasted: 15490 bytes (70%) 1055504497: 59: final user memory space: basic 0, divided 6, 21858 bytes 1055504497: 59: final admin overhead: basic 1, divided 3, 16384 bytes (40%) 1055504497: 59: final external space: 0 bytes (0 blocks) 1055504497: 59: top 10 allocations: 1055504497: 59: total-size count in-use-size count source 1055504497: 59: 1024 1 1024 1 ra=0x420b3c98 1055504497: 59: 1008 26 1008 26 ra=0x420f5495 1055504497: 59: 728 2 0 0 ra=0x4206bb71 1055504497: 59: 554 1 554 1 ra=0x40008a0c 1055504497: 59: 242 15 242 15 ra=0x420f5908 1055504497: 59: 128 1 128 1 ra=0x4000c78c 1055504497: 59: 120 1 0 0 ra=0x4206c9df 1055504497: 59: 28 1 28 1 ra=0x4000a3fe 1055504497: 59: 24 1 0 0 ra=0x4211554b 1055504497: 59: 3946 55 3074 51 Total of 15 1055504497: 59: dumping not-freed pointers changed since 0: 1055504497: 59: not freed: '0x804c048|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c088|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c0c8|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c108|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c148|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c188|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c1c8|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c208|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c248|s1' (36 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c288|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c2c8|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c308|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c348|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c388|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c3c8|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c408|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c448|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c488|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c4c8|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c508|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c548|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c588|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c5c8|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c608|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c648|s1' (38 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c688|s1' (40 bytes) from 'ra=0x420f5495' 1055504497: 59: not freed: '0x804c6c8|s1' (23 bytes) from 'ra=0x40006078' 1055504497: 59: not freed: '0x804c708|s1' (23 bytes) from 'ra=0x40008c2c' 1055504497: 59: not freed: '0x804c748|s1' (28 bytes) from 'ra=0x4000a3fe' 1055504497: 59: not freed: '0x804d008|s1' (1024 bytes) from 'ra=0x420b3c98' 1055504497: 59: not freed: '0x8050008|s1' (8 bytes) from 'ra=0x420f5335' 1055504497: 59: not freed: '0x8050028|s1' (15 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050048|s1' (15 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050068|s1' (14 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050088|s1' (14 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80500a8|s1' (19 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80500c8|s1' (15 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80500e8|s1' (17 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050108|s1' (17 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050128|s1' (18 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050148|s1' (12 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050168|s1' (17 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050188|s1' (17 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80501a8|s1' (18 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80501c8|s1' (18 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x80501e8|s1' (16 bytes) from 'ra=0x420f5908' 1055504497: 59: not freed: '0x8050208|s1' (16 bytes) from 'ra=0x420e5aed' 1055504497: 59: not freed: '0x8050228|s1' (8 bytes) from 'ra=0x420f4d8b' 1055504497: 59: not freed: '0x8050248|s1' (12 bytes) from 'ra=0x420f59cc' 1055504497: 59: not freed: '0x8051108|s1' (128 bytes) from 'ra=0x4000c78c' 1055504497: 59: not freed: '0x8053008|s1' (554 bytes) from 'ra=0x40008a0c' 1055504497: 59: total-size count source 1055504497: 59: 1024 1 ra=0x420b3c98 1055504497: 59: 1008 26 ra=0x420f5495 1055504497: 59: 554 1 ra=0x40008a0c 1055504497: 59: 242 15 ra=0x420f5908 1055504497: 59: 128 1 ra=0x4000c78c 1055504497: 59: 28 1 ra=0x4000a3fe 1055504497: 59: 23 1 ra=0x40006078 1055504497: 59: 23 1 ra=0x40008c2c 1055504497: 59: 16 1 ra=0x420e5aed 1055504497: 59: 12 1 ra=0x420f59cc 1055504497: 59: 8 1 ra=0x420f4d8b 1055504497: 59: 8 1 ra=0x420f5335 1055504497: 59: 3074 51 Total of 12 1055504497: 59: unknown memory: 51 pointers, 3074 bytes 1055504497: 59: ending time = 1055504497, elapsed since start = 0:00:00
Forums:
Memory leak
여러가지 모호한점이 좀 있군요
http://groups.google.co.kr/groups?hl=ko&lr=&ie=UTF-8&oe=UTF-8&newwindow=1&threadm=slrn8f9uk6.6lt.kaz%40ashi.FootPrints.net&rnum=8&prev=/groups%3Fq%3Dgetpwuid%2Bmemory%2Bleak%26hl%3Dko%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26newwindow%3D1%26start%3D0%26sa%3DN
을 참고하세요.
dmalloc 에 대해서는 잘 모르겠는데요.. solaris2.7 에서
dmalloc 에 대해서는 잘 모르겠는데요.. solaris2.7 에서 man getpwuid 하니
이런 말이 있네여. static storage 를 쓴다는 군요. 그러면 call 할때마다 덮어 쓰겠죠. 그래서 thread unsafe 하다는 말인듯 하고요. 그래서 이걸 memory leak 이라고 하지는 않을듯 하겠네요. 제가 해석 한 바로는 그렇네요.
나도 질문 : 제 생각으로는 현재 * 리턴하는 library function 은 거의 static 을 쓸듯한 느낌이 팍~들고 그래야 할것 같기도 하구요. 그걸 반드시 free 해라고 그러기에도 좀 그럴듯 하고요. 그러면 거의 thread 에서 여러번 콜하면 unsafe 하지 않을까요? 그러면 safe 한 libraray 를 별도로 만들어야 하지 않나요? 제 생각으로는 그렇게 밖에 답이 않나오네요.........
/** 주워 들은 것도 내 것이다. 많이 주워 들어야지*/
댓글 달기