Memory leak while using pthread_cancel()

I tried to execute a sample pthread program to cancel a newly created one using pthread_cancel(). but using valgrind on my code shows some memory leak.
My Code:
#include "iostream"
#include "unistd.h"
#include "pthread.h"
#include "signal.h"
using namespace std;
void handler(int);
void* testFun(void *arg)
{
cout<<"Iam child process \n";
sleep(2);
pthread_exit(NULL);
}
int main()
{
pthread_t threads;
int i=5;
int p=&i;
pthread_create(&threads, NULL, testFun,(void )&i);
cout<<"Main thread cancelling the child\n";
pthread_cancel(threads);
cout<<"waiting for child\n";
pthread_join(threads,(void
)&p);
cout<<"main thread exiting\n bye :)";
}

Valgrind output:
==16040== Memcheck, a memory error detector.
==16040== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==16040== Using LibVEX rev 1732, a library for dynamic binary translation.
==16040== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==16040== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==16040== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==16040== For more details, rerun with: -v
==16040==
Iam child process
Main thread cancelling the child
waiting for child
main thread exiting
bye :)==16040==
==16040== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 1)
==16040== malloc/free: in use at exit: 28 bytes in 1 blocks.
==16040== malloc/free: 2 allocs, 1 frees, 172 bytes allocated.
==16040== For counts of detected errors, rerun with: -v
==16040== searching for pointers to 1 not-freed blocks.
==16040== checked 104,232 bytes.
==16040==
==16040== 28 bytes in 1 blocks are still reachable in loss record 1 of 1
==16040== at 0x4022525: malloc (vg_replace_malloc.c:149)
==16040== by 0x400CDD2: _dl_map_object_deps (in /lib/ld-2.7.so)
==16040== by 0x4011ECC: dl_open_worker (in /lib/ld-2.7.so)
==16040== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==16040== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==16040== by 0x42835D1: do_dlopen (in /lib/libc-2.7.so)
==16040== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==16040== by 0x4283784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==16040== by 0x416D206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==16040== by 0x4169648: pthread_cancel (in /lib/libpthread-2.7.so)
==16040== by 0x80487F4: main (test.cpp:36)
==16040==
==16040== LEAK SUMMARY:
==16040== definitely lost: 0 bytes in 0 blocks.
==16040== possibly lost: 0 bytes in 0 blocks.
==16040== still reachable: 28 bytes in 1 blocks.
==16040== suppressed: 0 bytes in 0 blocks.
I dont know where these 28 bytes are lost.:confused:
how to rectify this leak????
thanks in advance

Out of curiosity, if you write the same thing as a C program, does it still leak?

hi all,

  1. do any body know this problem is related to which library(glibc-2.7 or pthread-2.7)?

  2. whether this problem is rectified in any of the latest releases?

Did you try it as a C program?

I compiled and executed it as a C program. but the result is still worse.

Valgrind Output:
==19464== Memcheck, a memory error detector.
==19464== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==19464== Using LibVEX rev 1732, a library for dynamic binary translation.
==19464== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==19464== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==19464== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==19464== For more details, rerun with: -v
==19464==
Iam child process
Main thread cancelling the child
waiting for child
main thread exiting
bye ==19464==
==19464== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==19464== malloc/free: in use at exit: 908 bytes in 5 blocks.
==19464== malloc/free: 6 allocs, 1 frees, 1,044 bytes allocated.
==19464== For counts of detected errors, rerun with: -v
==19464== searching for pointers to 5 not-freed blocks.
==19464== checked 64,144 bytes.
==19464==
==19464== 19 bytes in 1 blocks are still reachable in loss record 1 of 5
==19464== at 0x4022525: malloc (vg_replace_malloc.c:149)
==19464== by 0x400AB37: _dl_new_object (in /lib/ld-2.7.so)
==19464== by 0x40060D0: _dl_map_object_from_fd (in /lib/ld-2.7.so)
==19464== by 0x40084FB: _dl_map_object (in /lib/ld-2.7.so)
==19464== by 0x4011E70: dl_open_worker (in /lib/ld-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==19464== by 0x41645D1: do_dlopen (in /lib/libc-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4164784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==19464== by 0x404F206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==19464== by 0x404B648: pthread_cancel (in /lib/libpthread-2.7.so)
==19464==
==19464==
==19464== 19 bytes in 1 blocks are still reachable in loss record 2 of 5
==19464== at 0x4022525: malloc (vg_replace_malloc.c:149)
==19464== by 0x4008A47: _dl_map_object (in /lib/ld-2.7.so)
==19464== by 0x4011E70: dl_open_worker (in /lib/ld-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==19464== by 0x41645D1: do_dlopen (in /lib/libc-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4164784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==19464== by 0x404F206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==19464== by 0x404B648: pthread_cancel (in /lib/libpthread-2.7.so)
==19464== by 0x80485F4: main (testc.c:20)
==19464==
==19464==
==19464== 28 bytes in 1 blocks are still reachable in loss record 3 of 5
==19464== at 0x4022525: malloc (vg_replace_malloc.c:149)
==19464== by 0x400CDD2: _dl_map_object_deps (in /lib/ld-2.7.so)
==19464== by 0x4011ECC: dl_open_worker (in /lib/ld-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==19464== by 0x41645D1: do_dlopen (in /lib/libc-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4164784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==19464== by 0x404F206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==19464== by 0x404B648: pthread_cancel (in /lib/libpthread-2.7.so)
==19464== by 0x80485F4: main (testc.c:20)
==19464==
==19464==
==19464== 224 bytes in 1 blocks are still reachable in loss record 4 of 5
==19464== at 0x4021864: calloc (vg_replace_malloc.c:279)
==19464== by 0x400F963: _dl_check_map_versions (in /lib/ld-2.7.so)
==19464== by 0x40121A0: dl_open_worker (in /lib/ld-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==19464== by 0x41645D1: do_dlopen (in /lib/libc-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4164784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==19464== by 0x404F206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==19464== by 0x404B648: pthread_cancel (in /lib/libpthread-2.7.so)
==19464== by 0x80485F4: main (testc.c:20)
==19464==
==19464==
==19464== 618 bytes in 1 blocks are still reachable in loss record 5 of 5
==19464== at 0x4021864: calloc (vg_replace_malloc.c:279)
==19464== by 0x400A8CA: _dl_new_object (in /lib/ld-2.7.so)
==19464== by 0x40060D0: _dl_map_object_from_fd (in /lib/ld-2.7.so)
==19464== by 0x40084FB: _dl_map_object (in /lib/ld-2.7.so)
==19464== by 0x4011E70: dl_open_worker (in /lib/ld-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4011851: _dl_open (in /lib/ld-2.7.so)
==19464== by 0x41645D1: do_dlopen (in /lib/libc-2.7.so)
==19464== by 0x400E1B5: _dl_catch_error (in /lib/ld-2.7.so)
==19464== by 0x4164784: __libc_dlopen_mode (in /lib/libc-2.7.so)
==19464== by 0x404F206: pthread_cancel_init (in /lib/libpthread-2.7.so)
==19464== by 0x404B648: pthread_cancel (in /lib/libpthread-2.7.so)
==19464==
==19464== LEAK SUMMARY:
==19464== definitely lost: 0 bytes in 0 blocks.
==19464== possibly lost: 0 bytes in 0 blocks.
==19464== still reachable: 908 bytes in 5 blocks.
==19464== suppressed: 0 bytes in 0 blocks.