【发布时间】:2011-04-19 22:16:16
【问题描述】:
本块中提到的所有函数都是库函数。如何纠正这种内存泄漏?
它列在“仍可访问”类别下。 (还有4个,非常相似,但大小不一)
630 bytes in 1 blocks are still reachable in loss record 5 of 5
at 0x4004F1B: calloc (vg_replace_malloc.c:418)
by 0x931CD2: _dl_new_object (dl-object.c:52)
by 0x92DD36: _dl_map_object_from_fd (dl-load.c:972)
by 0x92EFB6: _dl_map_object (dl-load.c:2251)
by 0x939F1B: dl_open_worker (dl-open.c:255)
by 0x935965: _dl_catch_error (dl-error.c:178)
by 0x9399C5: _dl_open (dl-open.c:584)
by 0xA64E31: do_dlopen (dl-libc.c:86)
by 0x935965: _dl_catch_error (dl-error.c:178)
by 0xA64FF4: __libc_dlopen_mode (dl-libc.c:47)
by 0xAE6086: pthread_cancel_init (unwind-forcedunwind.c:53)
by 0xAE61FC: _Unwind_ForcedUnwind (unwind-forcedunwind.c:126)
Catch:一旦我运行我的程序,它就没有出现内存泄漏,但它在 Valgrind 输出中多了一行,这是以前不存在的:
丢弃 0x5296fa0-0x52af438 处的符号 在 /lib/libgcc_s-4.4.4-20100630.so.1 由于 munmap()
如果泄漏无法纠正,至少有人可以解释为什么 munmap() 行导致 Valgrind 报告 0“仍然可达”泄漏?
编辑:
这是一个最小的测试样本:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *runner(void *param) {
/* some operations ... */
pthread_exit(NULL);
}
int n;
int main(void) {
int i;
pthread_t *threadIdArray;
n=10; /* for example */
threadIdArray = malloc((n+n-1)*sizeof(pthread_t));
for(i=0;i<(n+n-1);i++) {
if( pthread_create(&threadIdArray[i],NULL,runner,NULL) != 0 ) {
printf("Couldn't create thread %d\n",i);
exit(1);
}
}
for(i=0;i<(n+n-1);i++) {
pthread_join(threadIdArray[i],NULL);
}
free(threadIdArray);
return(0);
}
运行:
valgrind -v --leak-check=full --show-reachable=yes ./a.out
【问题讨论】: