泄漏内存的pthreads [英] Leaking memory with pthreads
问题描述
我使用pthreads的,并根据Valgrind的,我泄漏内存,像<一个href=\"http://stackoverflow.com/questions/5610677/valgrind-memory-leak-errors-when-using-pthread-create\">valgrind内存泄漏错误时使用pthread_create的
I'm using pthreads and according to valgrind I am leaking memory, like in valgrind memory leak errors when using pthread_create
顶端回答说,如果你在pthread_join这一切的内存将被回收的线程,但它不是我的。
The top answer says that if you pthread_join all the threads this memory will be reclaimed, but it isn't for me.
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的输出
valgrind output
==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)
我是不是做错了什么或者是顶级的答案不正确的?
Am I doing something wrong or is the top answer incorrect?
推荐答案
这是不是一个实际的泄漏,你可以放心地忽略它。该内存块在pthread_create
分配用于扩展线程堆栈,它是不会释放。该库可以使用相同的内存区域未来可能调用在pthread_create
。
This is not an actual leak and you can safely ignore it. The memory block pthread_create
allocates is used for extending the thread stack and it is not freed. The library may use the same memory region for possible future calls to pthread_create
.
这篇关于泄漏内存的pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!