I'm doing a program in C that use threads that I will never know when they finish, so I'm using pthread_detach() to deallocate that memory, right?
Here's the code:
pthread_t thread_id_Gruder, thread_id_Tavish;
estado_threadGruder = pthread_create(&thread_id_Gruder,NULL,SERVER_McGruderDedicado,&td_Gruder);
estado_threadTavish = pthread_create(&thread_id_Tavish,NULL,SERVER_McTavishDedicado,&td_Tavish);
if (estado_threadGruder != 0 || estado_threadTavish != 0) {
if (estado_threadGruder != 0) MSSG_error(THREAD_ERROR, "McGruder");
else MSSG_error(THREAD_ERROR, "McTavish");
raise(SIGINT);
pause();
}
pthread_detach(thread_id_Gruder);
pthread_detach(thread_id_Tavish);
But when I use valgrind to check memory leaks I found this output:
HEAP SUMMARY:
==19426== in use at exit: 544 bytes in 2 blocks
==19426== total heap usage: 15 allocs, 13 frees, 628 bytes allocated
==19426==
==19426== Thread 1:
==19426== 272 bytes in 1 blocks are possibly lost in loss record 1 of 2
==19426== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19426== by 0x40134A6: allocate_dtv (dl-tls.c:286)
==19426== by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
==19426== by 0x4E44227: allocate_stack (allocatestack.c:627)
==19426== by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==19426== by 0x1097AA: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
==19426==
==19426== 272 bytes in 1 blocks are possibly lost in loss record 2 of 2
==19426== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19426== by 0x40134A6: allocate_dtv (dl-tls.c:286)
==19426== by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
==19426== by 0x4E44227: allocate_stack (allocatestack.c:627)
==19426== by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
==19426== by 0x1097CC: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
==19426==
==19426== LEAK SUMMARY:
==19426== definitely lost: 0 bytes in 0 blocks
==19426== indirectly lost: 0 bytes in 0 blocks
==19426== possibly lost: 544 bytes in 2 blocks
==19426== still reachable: 0 bytes in 0 blocks
==19426== suppressed: 0 bytes in 0 blocks
==19426==
==19426== For counts of detected and suppressed errors, rerun with: -v
==19426== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
I tried to run the code without the phtread_detach() function but valgrind shows the same memory leak, so I'm assuming I don't detach properly...
Am I detaching correctly the threads or maybe the problem is inside my threads?
Thanks.