2011-06-13 8 views
8

나는 스레드를 만드는 간단한 프로그램을 가지고 있으며이 스레드가 끝날 때까지 기다린 다음 프로그램도 종료됩니다. 이 프로그램을 C (gcc) 컴파일러로 컴파일하고 valgrind로 확인하면 문제가 발생하지 않지만 C++ (g ++) 컴파일러로 컴파일하고 valgrind로 확인하면 프로그램에 메모리 누수가 있음을 알 수 있습니다. 무엇이 문제 일 수 있습니까? 나는 ++ g을 사용하여 컴파일하고 내가 잘못 뭘하는지,메모리 누수 문제

==7658== HEAP SUMMARY: 
==7658==  in use at exit: 28 bytes in 1 blocks 
==7658== total heap usage: 2 allocs, 1 frees, 172 bytes allocated 
==7658== 
==7658== 28 bytes in 1 blocks are still reachable in loss record 1 of 1 
==7658== at 0x4024C1C: malloc (vg_replace_malloc.c:195) 
==7658== by 0x400C01E: _dl_map_object_deps (dl-deps.c:506) 
==7658== by 0x40117E0: dl_open_worker (dl-open.c:297) 
==7658== by 0x400D485: _dl_catch_error (dl-error.c:178) 
==7658== by 0x401119F: _dl_open (dl-open.c:586) 
==7658== by 0x428D0C1: do_dlopen (dl-libc.c:86) 
==7658== by 0x400D485: _dl_catch_error (dl-error.c:178) 
==7658== by 0x428D1C0: dlerror_run (dl-libc.c:47) 
==7658== by 0x428D2DA: __libc_dlopen_mode (dl-libc.c:160) 
==7658== by 0x4048876: pthread_cancel_init (unwind-forcedunwind.c:53) 
==7658== by 0x40489EC: _Unwind_ForcedUnwind (unwind-forcedunwind.c:126) 
==7658== by 0x40464B7: __pthread_unwind (unwind.c:130) 
==7658== 
==7658== LEAK SUMMARY: 
==7658== definitely lost: 0 bytes in 0 blocks 
==7658== indirectly lost: 0 bytes in 0 blocks 
==7658==  possibly lost: 0 bytes in 0 blocks 
==7658== still reachable: 28 bytes in 1 blocks 
==7658==   suppressed: 0 bytes in 0 blocks 

그래서 Valgrind의 체크인 할 때 여기

, 이것은 결과

#include <pthread.h> 
#include <errno.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 


unsigned char b = 0; 


void* threadfunc1(void *pVoid) 
{ 
    while(b == 0) 
    { 
    usleep(10000); 
    } 
    pthread_exit(0); 
} 



int main(void) 
{ 

    int status; 
    pthread_attr_t tattr; 
    pthread_t thread1; 

    status = pthread_attr_init(&tattr); 
    status = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_JOINABLE); 
    status = pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM); 

    if(pthread_create(&thread1, &tattr, threadfunc1, NULL) != 0) 
    { 
     exit(1); 
    } 

    usleep(1000000); 
    b = 1; 
    pthread_join(thread1, NULL); 
    usleep(2000000); 

    return 0; 
} 

내 프로그램입니다, 그것은이다 내 오류 또는 ... 왜 gcc를 사용하여 컴파일 할 때 문제가 발생하지 않고 C++ 메모리 누출을 사용하여 컴파일 할 때 발생합니까?

+0

:

는하지만에 대한 호출을 볼니까. 더 많은 문제가 예상됩니다. C 또는 C++ 중 하나만 사용하는 것이 좋습니다. – pmg

+0

제 친구로부터이 프로그램을 얻었습니다. makefile을 통해 컴파일했지만,이 프로그램의 NetBeans에서 새 프로젝트를 만들었습니다. 기본 컴파일러는 gcc이므로 컴파일 할 때 아무런 문제가 없었지만 makefile을 통해 컴파일 할 때 문제가 없었습니다. , 나는이 누출을 가지고있다. 그래서 왜 그런지를 아는 것이 흥미로웠다. – akmal

답변

6

프로그램이 메모리 누수가없는, 당신은 하지는 메모리 누수을 의미 하는가

==7658== definitely lost: 0 bytes in 0 blocks 
==7658== indirectly lost: 0 bytes in 0 blocks 
==7658==  possibly lost: 0 bytes in 0 blocks 

"아직 도달"이있다.

valgrind가 "여전히 도달 할 수있다"는 것에 대해 많은 질문이 있습니다. 그들 중 일부 :

7

@Kiril 키로프의 대답으로 이미 프로그램에는 메모리 누수가 없습니다 지적했다. 다중 언어 소스 파일을 작성하는 것은 어렵다

int pthread_attr_destroy(pthread_attr_t *attr); 
+0

pthread_attr_destroy에 관해서,이 질문을 편집 할 때 삭제 한 것 같습니다;). 귀하의 답변 주셔서 감사합니다! – akmal

+1

@akmal : 통화가 누락 된 경우에도 누수가 표시되지 않는 이유는 다음과 같습니다. –