4

pthreads を使用していますが、valgrind によると、pthread_create を使用した場合の valgrind メモリ リーク エラーのように、メモリ リークが発生しています。

一番上の答えは、すべてのスレッドを pthread_join すると、このメモリが再利用されると言っていますが、それは私のためではありません。

pthread_t threads[NUM_THREADS];
...
for (i = 0; i < NUM_THREADS; i++) {
    pthread_create(&threads[i], &attr, Worker, NULL);
}
...
for (i = 0; i < NUM_THREADS; i++) {
    pthread_join(threads[i], NULL);
}

valgrind 出力

==2707== HEAP SUMMARY:
==2707==     in use at exit: 954 bytes in 4 blocks
==2707==   total heap usage: 7,717 allocs, 7,713 frees, 79,563 bytes allocated
==2707== 
==2707== 34 bytes in 1 blocks are still reachable in loss record 1 of 4
==2707==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2707==    by 0x400541E: local_strdup (dl-load.c:162)
==2707==    by 0x40085D3: _dl_map_object (dl-load.c:2473)
==2707==    by 0x4012D5C: dl_open_worker (dl-open.c:225)
==2707==    by 0x400ECBE: _dl_catch_error (dl-error.c:178)
==2707==    by 0x4184D40: do_dlopen (dl-libc.c:89)
==2707==    by 0x404CD4B: start_thread (pthread_create.c:308)
==2707==    by 0x414BACD: clone (clone.S:130)
==2707== 
==2707== 34 bytes in 1 blocks are still reachable in loss record 2 of 4
==2707==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2707==    by 0x400B05A: _dl_new_object (dl-object.c:161)
==2707==    by 0x4006437: _dl_map_object_from_fd (dl-load.c:1051)
==2707==    by 0x400839F: _dl_map_object (dl-load.c:2568)
==2707==    by 0x4012D5C: dl_open_worker (dl-open.c:225)
==2707==    by 0x400ECBE: _dl_catch_error (dl-error.c:178)
==2707==    by 0x4184D40: do_dlopen (dl-libc.c:89)
==2707==    by 0x404CD4B: start_thread (pthread_create.c:308)
==2707==    by 0x414BACD: clone (clone.S:130)
==2707== 
==2707== 256 bytes in 1 blocks are still reachable in loss record 3 of 4
==2707==    at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2707==    by 0x401045C: _dl_check_map_versions (dl-version.c:300)
==2707==    by 0x401336A: dl_open_worker (dl-open.c:268)
==2707==    by 0x400ECBE: _dl_catch_error (dl-error.c:178)
==2707==    by 0x4184D40: do_dlopen (dl-libc.c:89)
==2707==    by 0x404CD4B: start_thread (pthread_create.c:308)
==2707==    by 0x414BACD: clone (clone.S:130)
==2707== 
==2707== 630 bytes in 1 blocks are still reachable in loss record 4 of 4
==2707==    at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2707==    by 0x400AE0F: _dl_new_object (dl-object.c:77)
==2707==    by 0x4006437: _dl_map_object_from_fd (dl-load.c:1051)
==2707==    by 0x400839F: _dl_map_object (dl-load.c:2568)
==2707==    by 0x4012D5C: dl_open_worker (dl-open.c:225)
==2707==    by 0x400ECBE: _dl_catch_error (dl-error.c:178)
==2707==    by 0x4184D40: do_dlopen (dl-libc.c:89)
==2707==    by 0x404CD4B: start_thread (pthread_create.c:308)
==2707==    by 0x414BACD: clone (clone.S:130)
==2707== 
==2707== LEAK SUMMARY:
==2707==    definitely lost: 0 bytes in 0 blocks
==2707==    indirectly lost: 0 bytes in 0 blocks
==2707==      possibly lost: 0 bytes in 0 blocks
==2707==    still reachable: 954 bytes in 4 blocks
==2707==         suppressed: 0 bytes in 0 blocks
==2707== 
==2707== For counts of detected and suppressed errors, rerun with: -v
==2707== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

私は何か間違ったことをしていますか、それともトップの答えは間違っていますか?

4

2 に答える 2

4

これは実際のリークではなく、無視しても問題ありません。pthread_create割り当てられたメモリ ブロックは、スレッド スタックを拡張するために使用され、解放されません。ライブラリは、今後の呼び出しで同じメモリ領域を使用する可能性がありますpthread_create

于 2012-10-30T05:07:13.127 に答える