用pthreads泄漏内存

时间:2012-10-30 04:59:35

标签: c pthreads valgrind

我正在使用pthreads并且根据valgrind我正在泄漏记忆,就像valgrind memory leak errors when using pthread_create

一样

最佳答案说,如果你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)

我做错了什么或者最佳答案是错误的吗?

2 个答案:

答案 0 :(得分:4)

这不是实际泄漏,您可以放心地忽略它。内存块pthread_create分配用于扩展线程堆栈,并且不会被释放。库可能会使用相同的内存区域,以便将来调用pthread_create

答案 1 :(得分:0)

我可能已经迟到了。正如所指出的,它不能有任何内存泄漏 - 我检查了你做了什么它是标准的创建方式(为线程创建内部存储器)和加入一个线程(自动回收这个内存)。

但是,您尚未显示attr的初始化。

您可以尝试:

pthread_create(&threads[i], NULL, Worker, NULL);

我在这里放置了NULL,而不是attr的任何值。 pthread初始化与属性紧密耦合(除非它以NULL形式提供)。

相关问题