Gelorakan …

November 26, 2007

pthread_create valgrind memory leak solved

Filed under: Code — gelorakan @ 3:56 am

Its look like pthread_detach() and pthread_create()   is couple like free() to  malloc() . So … dont forget !

#include <stdio.h>
#include <pthread.h>

// unmark pthread_detach(), memory leak will be gone

void* process( void* data ) {
printf( “hello world\n” );
}

main() {
pthread_t th;
pthread_create( &th, NULL, process, NULL );
sleep( 1 );
// pthread_detach( th );
return 0;
}

/*

$ valgrind –tool=memcheck –leak-check=full –show-reachable=yes ./a.out

==23782== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==23782== at 0x4C1DCBF: calloc (vg_replace_malloc.c:279)
==23782== by 0x400FDF2: _dl_allocate_tls (in /lib64/ld-2.5.so)
==23782== by 0x4E28A33: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.5.so)
==23782== by 0x4005BF: main (thsimple.c:11)
==23782==
==23782== LEAK SUMMARY:
==23782== definitely lost: 0 bytes in 0 blocks.
==23782== possibly lost: 272 bytes in 1 blocks.
==23782== still reachable: 0 bytes in 0 blocks.
==23782== suppressed: 0 bytes in 0 blocks.

*/

Links:

http://sourceware.org/ml/glibc-bugs/2007-04/msg00036.html

http://www.linuxquestions.org/questions/programming-9/memory-leak-in-pthread-591263/

http://sources.redhat.com/bugzilla/show_bug.cgi?id=5171

About these ads

6 Comments »

  1. As an update to this you even need to call pthread_detach even if you create the thread in the detached state

    this is the code I use and until I added the pthread_detach I had a memory leak

    pthread_attr_t pthread_attr = {0};
    
    // default threading attributes
    pthread_attr_init(&pthread_attr);
    
    // allow a thread to exit cleanly without a join
    pthread_attr_setdetachstate (&pthread_attr,PTHREAD_CREATE_DETACHED);
    
    pthread_attr_setstacksize(&pthread_attr, ThreadStackSize );
    
    pthread_t thread_id = 0;
    pthread_create(&thread_id,&pthread_attr,start_address,(void *)thread_param);
    
    // prevent a memory leak
    pthread_detach(thread_id);
    	
    pthread_attr_destroy(&pthread_attr);
    

    Comment by Developer_chris — June 13, 2008 @ 11:27 am | Reply

  2. This does not work for me!?

    Valgrind always possibly lost at pthread_create!

    detached the thread via set_deatchedstate

    and additionally i detached it like you say here, no change !

    Please whats up with this?

    Comment by dimi — July 25, 2008 @ 6:39 pm | Reply

  3. Try pthread_detach( thread_id ) instead of set_detachstate

    Or can you paste your code here so we can see what’s wrong

    Comment by gelorakan — July 28, 2008 @ 3:18 am | Reply

  4. i tried this example .. exactly this code from your web page

    results:
    ==00:00:00:00.426 28199==
    20 ==00:00:00:00.426 28199== 288 bytes in 1 blocks are possibly lost in loss record 1 of 1
    21 ==00:00:00:00.427 28199== at 0x4C220BC: calloc (vg_replace_malloc.c:397)
    22 ==00:00:00:00.427 28199== by 0x401132B: _dl_allocate_tls (in /lib/ld-2.7.so)
    23 ==00:00:00:00.427 28199== by 0x4E2DB87: pthread_create@@GLIBC_2.2.5 (in /lib/libpthread-2.7.so)
    24 ==00:00:00:00.427 28199== by 0×400627: main (in /home/mstragowski/a.out)

    Comment by noder — October 28, 2008 @ 11:32 am | Reply

  5. [...] [...]

    Pingback by Memory leak on pthread_create? — September 3, 2011 @ 2:40 am | Reply

  6. Calling pthread_create and then pthread_detach from the main thread leaves a window where the thread can run to completion before the detach gets called. Better to call pthread_detach( pthread_self() ); from the entry point of the thread – i.e. the first line in process()

    Comment by anon — January 18, 2012 @ 8:14 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: