3

I've a problem with valgrind errors about loss memory. This is my code:

if((err = pthread_create(&handlert, NULL, &handler, NULL)) != 0) perror(..)

if((err = pthread_create(&mastert , NULL, &createmaster, NULL)) != 0) perror(..)

for(int i = 0; i < THREADSINPOOL; i++) {
    if((err = pthread_create(&(f[i]), NULL, &createpool, NULL)) != 0) perror(..)
}

if((err = pthread_join(handlert,(void*) &sRet[1])) != 0) perror(..)

if((err = pthread_join(mastert,(void*) &lRet[1])) != 0) perror(..)

for(int i = 0; i < THREADSINPOOL; i++) {
    if((err = pthread_join(f[i], (void*) &wRet[i])) != 0) perror(..)
}
return 0;

I've a join for each thread and I'm checking the result but valgrind still say:

==21610== 560 bytes in 1 blocks are possibly lost in loss record 8 of 12
==21610==    at 0x4C2CC90: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21610==    by 0x4012E44: allocate_dtv (dl-tls.c:296)
==21610==    by 0x4012E44: _dl_allocate_tls (dl-tls.c:460)
==21610==    by 0x4E3FCC0: allocate_stack (allocatestack.c:589)
==21610==    by 0x4E3FCC0: pthread_create@@GLIBC_2.2.5 (pthread_create.c:495)
==21610==    by 0x401B3B: main (myfile.c:85)

(Same error for every pthread create, changing only the line of the code) Thank you in advance!

EDIT: flags in compilation: --leak-check=full -std=c99 -Wall -pedantic -g -DMAKE_VALGRIND_HAPPY There is only this error (don't look like a consequence of previous errors)

1 Answers1

3

I don't think this is a real leak, or if it is it's most likely in low-level code. And it's debatable if it should be considered a leak or an eager optimization.

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

It is not a real leak. As far as i know The buffer allocated at pthread_create() is used to extend the stack of threads. If you pthread_join() and pthread_create() again The old position in the stack will be used by the new one.

This is also alluded to in https://stackoverflow.com/a/17642468/714501

some implementations of POSIX threads (I'm guessing you're using glibc/NPTL) cache and reuse thread resources rather than freeing them fully.

I think for this case you could install a valgrind suppression:

http://valgrind.org/docs/manual/mc-manual.html#mc-manual.suppfiles

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Hello! Thanks for the fast anwer! Unfortunately I can't suppress valgrind.. This is a project for university and they test valgrind results (no valgrind errors are allowed) so I have to delete this error somehow! – Elena Guidi Jan 30 '19 at 08:25
  • @ElenaGuidi Interesting. Well there's always a workaround. Easy question first - do they let you link against a different libc? E.g. https://www.musl-libc.org/ – cnicutar Jan 30 '19 at 08:28
  • Good question! They say we have to use things done in the course.. and we have not study this library so I think is not allowed. – Elena Guidi Jan 30 '19 at 08:32
  • @ElenaGuidi You may want to talk to your professor and ask what exact version of gcc, valgrind, libc, ... you should use. Then if this doesn't work, talk to them again and demonstrate that it doesn't work. I don't get any valgrind errors with pthreads programs on a stock Ubuntu 18, so you may want to try that. – n. m. could be an AI Jan 30 '19 at 09:06
  • @ElenaGuidi Another option is to use a version where you provide the stack. Try http://man7.org/linux/man-pages/man3/pthread_attr_setstack.3.html to see if it still triggers an error. – cnicutar Jan 30 '19 at 11:21