pthread_create valgrind memory leak solved

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/
==23782== by 0x4E28A33: pthread_create@@GLIBC_2.2.5 (in /lib64/
==23782== by 0x4005BF: main (thsimple.c:11)
==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.



6 thoughts on “pthread_create valgrind memory leak solved

  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
    // 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
  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?

  3. Try pthread_detach( thread_id ) instead of set_detachstate

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

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

    ==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/
    23 ==00:00:00:00.427 28199== by 0x4E2DB87: pthread_create@@GLIBC_2.2.5 (in /lib/
    24 ==00:00:00:00.427 28199== by 0x400627: main (in /home/mstragowski/a.out)

  5. Pingback: Memory leak on pthread_create?

  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()

Leave a Reply

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

You are commenting using your 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