pthread와 valgrind에서의 memory leak

peccavi의 이미지

안녕하세요.

다음 코드를 valgrind로 돌려보면 memory leak이 발생합니다.

어디가 문제인건가요?

#include <stdio.h>
#include <pthread.h>
 
void *thread_routine(void *arg)
{
	return arg;
}
 
int main()
{
	int ret;
	void *result;
	pthread_t ThreadID;
 
	ret = pthread_create(&ThreadID, NULL, thread_routine, NULL);
	if( ret != 0 )
	{
		printf("create thread fail\n");
		return 0;
	}
 
	ret = pthread_join(ThreadID, &result);
	if( ret != 0 )
	{
		printf("pthread_join error\n");
		return 0;
	}
 
	return 0;
}

valgrind 결과

==7480== Memcheck, a memory error detector.
==7480== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==7480== Using LibVEX rev 1471, a library for dynamic binary translation.
==7480== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==7480== Using valgrind-3.1.0-Debian, a dynamic binary instrumentation framework.
==7480== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==7480== For more details, rerun with: -v
==7480==
==7480==
==7480== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==7480== malloc/free: in use at exit: 68 bytes in 1 blocks.
==7480== malloc/free: 1 allocs, 0 frees, 68 bytes allocated.
==7480== For counts of detected errors, rerun with: -v
==7480== searching for pointers to 1 not-freed blocks.
==7480== checked 8,480,624 bytes.
==7480==
==7480== LEAK SUMMARY:
==7480==    definitely lost: 0 bytes in 0 blocks.
==7480==      possibly lost: 68 bytes in 1 blocks.
==7480==    still reachable: 0 bytes in 0 blocks.
==7480==         suppressed: 0 bytes in 0 blocks.
==7480== Reachable blocks (those to which a pointer was found) are not shown.
==7480== To see them, rerun with: --show-reachable=yes

gcc 버전은 4.0.3 입니다.
뭐가 문제인지, 이게 실제로 문제가 되는건지 당췌 모르겠습니다. -_-;;
답변좀 부탁드리겠습니다~
감기조심하세요~~

cinsk의 이미지

특별히 문제가 있어 보이지는 않습니다. 가끔 여러 라이브러리를 쓴 경우, 라이브러리에서 내부적으로 쓰고 있는 resource가 있거나, 라이브러리에 버그?가 있어서 leak로 보고되는 것이 있긴 하지만, 이 프로그램에서 발생한 것이 아니기 때문에 크게 걱정할 필요는 없을 것 같습니다.

또, valgrind로 test할 프로그램에 debugging 정보를 주고 (컴파일 옵션 -g), valgrind를 실행할 때, 다음과 같이 실행하면 자세한 정보를 얻을 수 있습니다:

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes -v a.out

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

peccavi의 이미지

세부 옵션으로 확인해본 결과 pthread_create 내부에서 문제가 있어 보이는데, 맞는건지요..

...
==19511== 68 bytes in 1 blocks are possibly lost in loss record 1 of 1
==19511==    at 0x401D7AA: calloc (vg_replace_malloc.c:279)
==19511==    by 0x400D790: (within /lib/ld-2.3.6.so)
==19511==    by 0x400DA52: _dl_allocate_tls (in /lib/ld-2.3.6.so)
==19511==    by 0x40338C6: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/cmov/libpthread-2.3.6.so)
==19511==    by 0x80484B5: main (main.c:18)
...

glibc나 커널의 다른 버전에서는 테스트해 보지는 않았지만
일단은 그냥 무시하고 지나가야 될듯 싶습니다.
답변 정말 감사드립니다.

----
jai guru deva om...

----
jai guru deva om...

twkwon의 이미지

pthread_detach 함수를 불러보세요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.