2016-07-25 117 views
2

我只是写了这个示例程序,看看如何将一个单向链表如何释放内存使用glib函数列表项

我试图释放内存上使用巧舌如簧的功能,但它表明,零释放..in下面的背景下..

当使用g_free(list)显示一个免费的,但同时使用的valgrind它显示无效免费..

如何释放内存,并没有任何错误时,Valgrind的?

谢谢

1 #include<headers.h> 
    2 int main() 
    3  { 
    4    printf("SINGLY LINKED LIST\n"); 
    5 
    6    GSList *list = NULL,*iterator = NULL; 
    7 
    8    list = g_slist_append(list,"First_Element"); 
    9    printf("1st--%p\n",list); 
10 
11    list = g_slist_prepend(list,"Zeroth_Element"); 
12    printf("0th--%p\n",list); 
13 
14    list = g_slist_append(list,"second_element"); 
15    printf("2nd--%p\n",list); 
16 
17    for(iterator = list;iterator;iterator = iterator->next) 
18      printf("%s\n",(char*)iterator->data); 
19 
20    g_slist_free(list); 
21  return 0; 
22  } 

bash-4.1$ valgrind --leak-check=full --show-reachable=yes ../bin/exe 
==8021== Memcheck, a memory error detector 
==8021== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. 
==8021== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info 
==8021== Command: ../bin/exe 
==8021== 
SINGLY LINKED LIST 
1st--0x58fff00 
0th--0x58fff10 
2nd--0x58fff10 
Zeroth_Element 
First_Element 
second_element 
==8021== 
==8021== HEAP SUMMARY: 
==8021==  in use at exit: 4,252 bytes in 8 blocks 
==8021== total heap usage: 8 allocs, 0 frees, 4,252 bytes allocated 
==8021== 
==8021== 240 bytes in 1 blocks are possibly lost in loss record 1 of 6 
==8021== at 0x4C26588: memalign (vg_replace_malloc.c:727) 
==8021== by 0x4C26623: posix_memalign (vg_replace_malloc.c:876) 
==8021== by 0x4E8D777: ??? (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8DFFB: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== 252 bytes in 1 blocks are still reachable in loss record 2 of 6 
==8021== at 0x4C267BB: calloc (vg_replace_malloc.c:593) 
==8021== by 0x4E78BF7: g_malloc0 (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8CFD1: ??? (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8DE8D: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== 504 bytes in 1 blocks are still reachable in loss record 3 of 6 
==8021== at 0x4C267BB: calloc (vg_replace_malloc.c:593) 
==8021== by 0x4E78BF7: g_malloc0 (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8CFF1: ??? (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8DE8D: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== 504 bytes in 1 blocks are still reachable in loss record 4 of 6 
==8021== at 0x4C267BB: calloc (vg_replace_malloc.c:593) 
==8021== by 0x4E78BF7: g_malloc0 (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8D011: ??? (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8DE8D: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== 720 bytes in 3 blocks are possibly lost in loss record 5 of 6 
==8021== at 0x4C26588: memalign (vg_replace_malloc.c:727) 
==8021== by 0x4C26623: posix_memalign (vg_replace_malloc.c:876) 
==8021== by 0x4E8D777: ??? (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8E031: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== 2,032 bytes in 1 blocks are still reachable in loss record 6 of 6 
==8021== at 0x4C267BB: calloc (vg_replace_malloc.c:593) 
==8021== by 0x4E78BF7: g_malloc0 (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8DE32: g_slice_alloc (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x4E8F612: g_slist_append (in /lib64/libglib-2.0.so.0.2800.8) 
==8021== by 0x400666: main (ex_main.c:8) 
==8021== 
==8021== LEAK SUMMARY: 
==8021== definitely lost: 0 bytes in 0 blocks 
==8021== indirectly lost: 0 bytes in 0 blocks 
==8021==  possibly lost: 960 bytes in 4 blocks 
==8021== still reachable: 3,292 bytes in 4 blocks 
==8021==   suppressed: 0 bytes in 0 blocks 
==8021== 
==8021== For counts of detected and suppressed errors, rerun with: -v 
==8021== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6) 
bash-4.1$ 

回答

1

你将永远不会得到valgrind上能说会道的内存使用情况如实报告,除非你找到并安装油嘴的具体排除的文件,告诉valgrind什么内存glib负责,哪些不考虑报告时使用。为什么? Glib为它自己的使用分配了相当多的内存,以及组成该工具包的所有列表的内存。该内存不在valgrind正常报告的范围之内。 Glib不是唯一包含valgrind问题的软件包,Mac OS一直不能很好地播放它。

为此,人们(通常是glib开发人员)将编写排除列表来解决与valgrind有关的内存报告问题。可能有多个列表需要,它必须匹配你的glib发行版,否则它将毫无价值。因此,搜索valgrind glib排除文件以匹配您的版本,并希望再次具有零均值零。