[질문] 스레드 프로그램을 하던중에.... (메모리 해제관련)

unexpected의 이미지

[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]

eminency의 이미지

위의 메시지는 직접 alloc하신 것이 아니라 pthread_create에서 쓰레드를 만들면서 할당받은 메모리를 의미합니다.

man pthread_join이나 man pthread_detach를 참고하세요.

쓰레드 내부에서 return이나 pthread_exit한다고 해서 쓰레드 자원이 해제되지는 않습니다.
위의 두 가지 중 하나를 수행하셔야 합니다.

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.