[질문] 스레드 프로그램을 하던중에.... (메모리 해제관련)
[code]
우선 항상 도움을 받고 있음에 감사를 드립니다.
------------------------------------------- sthread.c ----------------------------
void *thread_main(void *ns);
int main()
{
pthread_t thread_id;
pthread_attr_t pattr;
int rv;
int i;
if (pthread_attr_init(&pattr) != 0) {
return -1;
}
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
pthread_attr_setscope(&pattr, PTHREAD_SCOPE_SYSTEM );
rv = pthread_create(&thread_id, (const pthread_attr_t *)&pattr, thread_main, NULL);
pthread_attr_destroy(&pattr);
if(rv != 0 ) {
return -1;
}
sleep(1);
return 0;
}
void *thread_main(void *ns)
{
ns = NULL;
printf("info(4) : %s, %s, %d : success.\n", __FILE__, __func__, __LINE__);
printf("info(4) : %s, %s, %d : exit thread.\n", __FILE__, __func__, __LINE__);
return NULL;
}
----------------------------------------------------------------------------------
위와 같은 샘플프로그램을 짰습니다.
제대로 된다고 생각을 했는데...
valgrind를 돌려보니...아래와 같이 메모리 해제가 안된 부분이 하나 나오네요.
코드르 보시면 아시겠지만~ 프로그램상에서 메모리 alloc 한 부분은 없습니다.
아래 로그를 보면 스레드함수에서 발생되었던데..
그것을 해결하려면 어떻게 해야 하나요~
----------------------------------------------------------------------------------
$ valgrind --leak-check=full ./sthread
==8714== Memcheck, a memory error detector.
==8714== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==8714== Using LibVEX rev 1575, a library for dynamic binary translation.
==8714== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==8714== Using valgrind-3.1.1, a dynamic binary instrumentation framework.
==8714== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==8714== For more details, rerun with: -v
==8714==
info(4) : sthread.c, thread_main, 54 : success.
info(4) : sthread.c, thread_main, 55 : exit thread.
==8714==
==8714== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 1)
==8714== malloc/free: in use at exit: 136 bytes in 1 blocks.
==8714== malloc/free: 1 allocs, 0 frees, 136 bytes allocated.
==8714== For counts of detected errors, rerun with: -v
==8714== searching for pointers to 1 not-freed blocks.
==8714== checked 10,567,436 bytes.
==8714==
==8714== 136 bytes in 1 blocks are possibly lost in loss record 1 of 1
==8714== at 0x40056BF: calloc (vg_replace_malloc.c:279)
==8714== by 0x33471A: _dl_allocate_tls (in /lib/ld-2.3.4.so)
==8714== by 0x5A491E: pthread_create@@GLIBC_2.1 (in /lib/tls/libpthread-2.3.4.so)
==8714== by 0x804852D: main (in /home/ksn/samples/sthread)
==8714==
==8714== LEAK SUMMARY:
==8714== definitely lost: 0 bytes in 0 blocks.
==8714== possibly lost: 136 bytes in 1 blocks.
==8714== still reachable: 0 bytes in 0 blocks.
==8714== suppressed: 0 bytes in 0 blocks.
==8714== Reachable blocks (those to which a pointer was found) are not shown.
==8714== To see them, rerun with: --show-reachable=yes
---------------------------------------------------------------------------------
위와 같은 결과가 나옵니다.
메모리가 프리가 안되었다고 나오는데.... 이유를 모르겠습니다.
도움주시면 감사하겠습니다....
[/code]
위의 메시지는 직접
위의 메시지는 직접 alloc하신 것이 아니라 pthread_create에서 쓰레드를 만들면서 할당받은 메모리를 의미합니다.
man pthread_join이나 man pthread_detach를 참고하세요.
쓰레드 내부에서 return이나 pthread_exit한다고 해서 쓰레드 자원이 해제되지는 않습니다.
위의 두 가지 중 하나를 수행하셔야 합니다.
노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5
댓글 달기