Why does memory leak occur when using pthread_create and pthread_detach ?
Issue
- When I use ‘pthread_exit’ and ‘pthread_detach’ in my code it shows some memory leak when valgrind report is taken.
#include <stdio.h>
#include <pthread.h>
void* process( void* data ) {
printf( "hello world\n" );
int i = 0;
pthread_exit(&i);
}
main() {
pthread_t th;
pthread_create( &th, NULL, process, NULL );
while(1) {
sleep( 5 );
}
pthread_detach( th );
return 0;
}
- I have used ‘pthread_exit’ and ‘pthread_detach’ but it shows some memory leak when valgrind report is taken
root@mercury06> valgrind --leak-check=full ./a.out
==23064== Memcheck, a memory error detector
==23064== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==23064== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==23064== Command: ./a.out
==23064==
hello world
^C==23064==
==23064== HEAP SUMMARY:
==23064== in use at exit: 272 bytes in 1 blocks
==23064== total heap usage: 6 allocs, 5 frees, 1,856 bytes allocated
==23064==
==23064== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==23064== at 0x4C2677B: calloc (vg_replace_malloc.c:593)
==23064== by 0x40118A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==23064== by 0x4E361E8: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==23064== by 0x40064F: main (in /root/a.out)
==23064==
==23064== LEAK SUMMARY:
==23064== definitely lost: 0 bytes in 0 blocks
==23064== indirectly lost: 0 bytes in 0 blocks
==23064== possibly lost: 272 bytes in 1 blocks
==23064== still reachable: 0 bytes in 0 blocks
==23064== suppressed: 0 bytes in 0 blocks
==23064==
==23064== For counts of detected and suppressed errors, rerun with: -v
==23064== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
Environment
- Red Hat Enterprise Linux 6
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.