pthread_join()をしなければメモリリークする。
はじめに
pthread_join()のManpageにあるこの一文を検証すべく、valgrindでメモリリークをチェックしてみた。
「合流可能なスレッドが終了しても、 別のスレッドがそのスレッドに対して pthread_join を呼び出すまでは、 そのメモリ資源 (スレッドディスクリプタとスタック) は解放されない。 したがって、メモリリークを防ぐためには、 合流可能なスレッドそれぞれに対して pthread_join を 1 回ずつ呼び出さなければならない。」
ソース
検証ソースは以下。
(github→https://github.com/tomohikoseven/pthread_leak)
pthread_join()あり
#include<stdio.h> #include<stdlib.h> #include<pthread.h> int test( ) { return 2; } int main( ) { pthread_t th_num; int ret; void* thread_return; ret = pthread_create( &th_num, NULL, ( void* )test, NULL ); if( 0 != ret ) { printf( "pthread_create error.\n" ); exit( 1 ); } ret = pthread_join( th_num, &thread_return ); if( 0 != ret ) { printf( "pthread_join error.\n" ); exit( 1 ); } return 0; }
pthread_join()なし
#include<stdio.h> #include<stdlib.h> #include<pthread.h> int test( ) { return 2; } int main( ) { pthread_t th_num; int ret; ret = pthread_create( &th_num, NULL, ( void* )test, NULL ); if( 0 != ret ) { printf( "pthread_create error.\n" ); exit( 1 ); } return 0; }
結果
andre@andre-VirtualBox:~/work/c/pthread_leak$ /usr/bin/valgrind --leak-check=full ./main_join ==21850== Memcheck, a memory error detector ==21850== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==21850== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info ==21850== Command: ./main_join ==21850== ==21850== ==21850== HEAP SUMMARY: ==21850== in use at exit: 0 bytes in 0 blocks ==21850== total heap usage: 1 allocs, 1 frees, 136 bytes allocated ==21850== ==21850== All heap blocks were freed -- no leaks are possible ==21850== ==21850== For counts of detected and suppressed errors, rerun with: -v ==21850== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 8) andre@andre-VirtualBox:~/work/c/pthread_leak$ /usr/bin/valgrind --leak-check=full ./main_no_join ==21870== Memcheck, a memory error detector ==21870== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==21870== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info ==21870== Command: ./main_no_join ==21870== ==21870== ==21870== HEAP SUMMARY: ==21870== in use at exit: 136 bytes in 1 blocks ==21870== total heap usage: 1 allocs, 0 frees, 136 bytes allocated ==21870== ==21870== 136 bytes in 1 blocks are possibly lost in loss record 1 of 1 ==21870== at 0x402732C: calloc (vg_replace_malloc.c:467) ==21870== by 0x4010B24: allocate_dtv (dl-tls.c:300) ==21870== by 0x40112D8: _dl_allocate_tls (dl-tls.c:464) ==21870== by 0x404F44E: pthread_create@@GLIBC_2.1 (allocatestack.c:571) ==21870== by 0x804865B: main (main_no_join.c:18) ==21870== ==21870== LEAK SUMMARY: ==21870== definitely lost: 0 bytes in 0 blocks ==21870== indirectly lost: 0 bytes in 0 blocks ==21870== possibly lost: 136 bytes in 1 blocks ==21870== still reachable: 0 bytes in 0 blocks ==21870== suppressed: 0 bytes in 0 blocks ==21870== ==21870== For counts of detected and suppressed errors, rerun with: -v ==21870== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from 8) andre@andre-VirtualBox:~/work/c/pthread_leak$
これ以上のことは、現在調査中。。。