Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why pthread causes a memory leak

Tags:

Whenever I create a pthread, valgrind outputs a memory leak,

For example the below code:

#include <stdio.h> #include <unistd.h> #include <pthread.h>   void *timer1_function (void *eit){   (void) eit;     printf("hello world\n");     pthread_exit(NULL); }  int main(void){    pthread_t timer1;    pthread_create( &timer1, NULL, timer1_function,  NULL);  ///////line13    int i=0;    for(i=0;i<2;i++){usleep(1);}    return 0; } 

valgrind outputs

==1395== HEAP SUMMARY: ==1395==     in use at exit: 136 bytes in 1 blocks ==1395==   total heap usage: 6 allocs, 5 frees, 1,134 bytes allocated ==1395==  ==1395== 136 bytes in 1 blocks are possibly lost in loss record 1 of 1 ==1395==    at 0x402A629: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==1395==    by 0x4011304: allocate_dtv (dl-tls.c:297) ==1395==    by 0x4011AAB: _dl_allocate_tls (dl-tls.c:461) ==1395==    by 0x4052470: pthread_create@@GLIBC_2.1 (allocatestack.c:571) ==1395==    by 0x8048566: main (test.c:13) ==1395==  ==1395== LEAK SUMMARY: ==1395==    definitely lost: 0 bytes in 0 blocks ==1395==    indirectly lost: 0 bytes in 0 blocks ==1395==      possibly lost: 136 bytes in 1 blocks ==1395==    still reachable: 0 bytes in 0 blocks ==1395==         suppressed: 0 bytes in 0 blocks 

why pthread_create cause a problem although I was using the man page as reference, and how can I fix it?

like image 904
Johan Elmander Avatar asked Jul 14 '13 18:07

Johan Elmander


People also ask

What is the main cause of memory leaks?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.

Can threads cause memory leaks?

Long running thread doesn't create memory leak. It is what you do inside it. Technically memory leaks happens when garbage collector could not collect free space, as the space is marked as being used.

How do you prevent thread leaks in memory?

Always join the joinable threads; by not joining them, you risk serious memory leaks. For example, a thread on Red Hat Enterprise Linux (RHEL4), needs a 10MB stack, which means at least 10MB is leaked if you haven't joined it. Say you design a manager-worker mode program to process incoming requests.

Can a memory leak happen in managed languages?

Common Types of Memory Leaks. Leaks in managed platforms are effectively references to an element that is no longer necessary. There are many samples of this, but they all boil down to discarding said reference. The most common problem is caching.


1 Answers

A thread is an allocated resource and you did not free it before exiting. You should call pthread_join; this would also eliminate the need for your hackish and incorrect sleep loop.

It's possible that even once you fix this, valgrind will still see a "leak", since some implementations of POSIX threads (I'm guessing you're using glibc/NPTL) cache and reuse thread resources rather than freeing them fully. I'm not sure if valgrind works around this or not.

like image 87
R.. GitHub STOP HELPING ICE Avatar answered Sep 23 '22 09:09

R.. GitHub STOP HELPING ICE