Memory leak in pthread_cancel with a detached thread.

G

guidoreina

Hi,

When I ran valgrind with this small application:

<code>
#include <unistd.h>
#include <pthread.h>

void *start_routine(void *arg)
{
sleep(60);
return 0;
}

int main()
{
pthread_attr_t attr;
pthread_t th;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

pthread_create(&th, &attr, start_routine, NULL);

pthread_attr_destroy(&attr);

sleep(1);

pthread_cancel(th);

return 0;
}
</code>

I get a memory leak. The child thread is detached so, to my
understanding, I shouldn't have any memory leak.

Now there is something really weird. If I compile it with gcc (as C
code), valgrind outputs:
==19013== Memcheck, a memory error detector.
==19013== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et
al.
==19013== Using LibVEX rev 1804, a library for dynamic binary
translation.
==19013== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==19013== Using valgrind-3.3.0-Debian, a dynamic binary
instrumentation framework.
==19013== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et
al.
==19013== For more details, rerun with: -v
==19013==
==19013== Invalid read of size 4
==19013== at 0x40151E3: (within /lib/ld-2.7.so)
==19013== by 0x4005C59: (within /lib/ld-2.7.so)
==19013== by 0x4007A87: (within /lib/ld-2.7.so)
==19013== by 0x4011533: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x804861A: main (thread.c:24)
==19013== Address 0x49a20f0 is 16 bytes inside a block of size 19
alloc'd
==19013== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
==19013== by 0x4008021: (within /lib/ld-2.7.so)
==19013== by 0x4011533: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x804861A: main (thread.c:24)
==19013==
==19013== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from
1)
==19013== malloc/free: in use at exit: 1,028 bytes in 6 blocks.
==19013== malloc/free: 6 allocs, 0 frees, 1,028 bytes allocated.
==19013== For counts of detected errors, rerun with: -v
==19013== searching for pointers to 6 not-freed blocks.
==19013== checked 8,472,668 bytes.
==19013==
==19013==
==19013== 19 bytes in 1 blocks are still reachable in loss record 1 of
6
==19013== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
==19013== by 0x400A13A: (within /lib/ld-2.7.so)
==19013== by 0x4005C59: (within /lib/ld-2.7.so)
==19013== by 0x4007A87: (within /lib/ld-2.7.so)
==19013== by 0x4011533: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013==
==19013==
==19013== 19 bytes in 1 blocks are still reachable in loss record 2 of
6
==19013== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
==19013== by 0x4008021: (within /lib/ld-2.7.so)
==19013== by 0x4011533: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x804861A: main (thread.c:24)
==19013==
==19013==
==19013== 28 bytes in 1 blocks are still reachable in loss record 3 of
6
==19013== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
==19013== by 0x400BF4F: (within /lib/ld-2.7.so)
==19013== by 0x4011593: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x804861A: main (thread.c:24)
==19013==
==19013==
==19013== 136 bytes in 1 blocks are possibly lost in loss record 4 of
6
==19013== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
==19013== by 0x4010617: (within /lib/ld-2.7.so)
==19013== by 0x40106DB: _dl_allocate_tls (in /lib/ld-2.7.so)
==19013== by 0x403DC3C: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/
cmov/libpthread-2.7.so)
==19013== by 0x80485F8: main (thread.c:18)
==19013==
==19013==
==19013== 208 bytes in 1 blocks are still reachable in loss record 5
of 6
==19013== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
==19013== by 0x400EED4: (within /lib/ld-2.7.so)
==19013== by 0x4011816: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x804861A: main (thread.c:24)
==19013==
==19013==
==19013== 618 bytes in 1 blocks are still reachable in loss record 6
of 6
==19013== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
==19013== by 0x4009EDA: (within /lib/ld-2.7.so)
==19013== by 0x4005C59: (within /lib/ld-2.7.so)
==19013== by 0x4007A87: (within /lib/ld-2.7.so)
==19013== by 0x4011533: (within /lib/ld-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x4010F4D: (within /lib/ld-2.7.so)
==19013== by 0x415E291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19013== by 0x400D5C5: (within /lib/ld-2.7.so)
==19013== by 0x415E454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19013== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19013==
==19013== LEAK SUMMARY:
==19013== definitely lost: 0 bytes in 0 blocks.
==19013== possibly lost: 136 bytes in 1 blocks.
==19013== still reachable: 892 bytes in 5 blocks.
==19013== suppressed: 0 bytes in 0 blocks.


But, if I compile it with g++, valgrind outputs:
==19096== Memcheck, a memory error detector.
==19096== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et
al.
==19096== Using LibVEX rev 1804, a library for dynamic binary
translation.
==19096== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==19096== Using valgrind-3.3.0-Debian, a dynamic binary
instrumentation framework.
==19096== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et
al.
==19096== For more details, rerun with: -v
==19096==
==19096==
==19096== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from
1)
==19096== malloc/free: in use at exit: 172 bytes in 2 blocks.
==19096== malloc/free: 2 allocs, 0 frees, 172 bytes allocated.
==19096== For counts of detected errors, rerun with: -v
==19096== searching for pointers to 2 not-freed blocks.
==19096== checked 8,512,780 bytes.
==19096==
==19096== 28 bytes in 1 blocks are still reachable in loss record 1 of
2
==19096== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
==19096== by 0x400BF4F: (within /lib/ld-2.7.so)
==19096== by 0x4011593: (within /lib/ld-2.7.so)
==19096== by 0x400D5C5: (within /lib/ld-2.7.so)
==19096== by 0x4010F4D: (within /lib/ld-2.7.so)
==19096== by 0x4282291: (within /lib/tls/i686/cmov/libc-2.7.so)
==19096== by 0x400D5C5: (within /lib/ld-2.7.so)
==19096== by 0x4282454: __libc_dlopen_mode (in /lib/tls/i686/cmov/
libc-2.7.so)
==19096== by 0x4046686: pthread_cancel_init (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19096== by 0x4042B18: pthread_cancel (in /lib/tls/i686/cmov/
libpthread-2.7.so)
==19096== by 0x80486DB: main (thread.c:24)
==19096==
==19096==
==19096== 144 bytes in 1 blocks are possibly lost in loss record 2 of
2
==19096== at 0x4021BDE: calloc (vg_replace_malloc.c:397)
==19096== by 0x4010617: (within /lib/ld-2.7.so)
==19096== by 0x40106DB: _dl_allocate_tls (in /lib/ld-2.7.so)
==19096== by 0x403DC3C: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/
cmov/libpthread-2.7.so)
==19096== by 0x80486B9: main (thread.c:18)
==19096==
==19096== LEAK SUMMARY:
==19096== definitely lost: 0 bytes in 0 blocks.
==19096== possibly lost: 144 bytes in 1 blocks.
==19096== still reachable: 28 bytes in 1 blocks.
==19096== suppressed: 0 bytes in 0 blocks.


The Invalid read is gone and the memory leak is smaller.

$ uname -a
Linux guido-laptop 2.6.24-21-generic #1 SMP Tue Oct 21 23:43:45 UTC
2008 i686 GNU/Linux
$ cat /etc/issue
Ubuntu 8.04.1 \n \l
$ dpkg -S /usr/lib/libc.so
libc6-dev: /usr/lib/libc.so

Should pthread_cancel leak something when cancelling a detached
thread?

Thanks in advance.
Guido
 
G

Guest

Hi,

When I ran valgrind with this small application:

<code>
#include <unistd.h>
#include <pthread.h>

<snip>

you need to post this to a newsgroup where unix and threading are
on-topic. maybe comp.unix.programming
 
G

guidoreina

<snip>

you need to post this to a newsgroup where unix and threading are
on-topic. maybe comp.unix.programming

Hi,

thank you for your answer. After writing in this newsgroup I found
comp.unix.programmer (too late, I know ;) ), so I also posted
there :). You are right, it is not the right newsgroup, it fits better
in the unix programming newsgroup :).

Thank you :),
Guido
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top