C 瓦尔格林。。。大小为8 free';D
在尝试释放列表后,我在Valgrind中遇到此错误。打印列表将列表转储到系统日志。我很有信心输出是正确的 瓦尔格林:C 瓦尔格林。。。大小为8 free';D,c,valgrind,C,Valgrind,在尝试释放列表后,我在Valgrind中遇到此错误。打印列表将列表转储到系统日志。我很有信心输出是正确的 瓦尔格林: ==7028== 1 errors in context 1 of 10: ==7028== Invalid read of size 4 ==7028== at 0x8049603: free_list (list.c:239) ==7028== by 0x80488B5: m61_close_for_valgrind (m61.c:36) ==7028==
==7028== 1 errors in context 1 of 10:
==7028== Invalid read of size 4
==7028== at 0x8049603: free_list (list.c:239)
==7028== by 0x80488B5: m61_close_for_valgrind (m61.c:36)
==7028== by 0x8048825: main (mytest.c:19)
==7028== Address 0x420006c is 4 bytes inside a block of size 8 free'd
==7028== at 0x4028F0F: free (vg_replace_malloc.c:446)
==7028== by 0x804960C: free_list (list.c:239)
==7028== by 0x80488B5: m61_close_for_valgrind (m61.c:36)
==7028== by 0x8048825: main (mytest.c:19)
==7028==
mytest.c:
15 char *temp = malloc(10);
16 char *temp2 = malloc(10);
17 free(temp);
18 free(temp2);
19 m61_close_for_valgrind();
清单h
typedef struct lnode {
ACTIVE_ALLOCATION *value;
struct lnode *next;
} lnode;
list.c(由m61_close_为_valgrind()调用)
系统日志
Sep 19 00:37:02 appliance mytest[7759]: -- Start List Dump --
Sep 19 00:37:02 appliance mytest[7759]: (*nptr)=0x903f220 (*nptr)->value=0x903f208 (*nptr)->next=0x903f260 (*nptr)->value->ptr=0x903f1f0
Sep 19 00:37:02 appliance mytest[7759]: (*nptr)->value->ptr=0x903f1f0
Sep 19 00:37:02 appliance mytest[7759]: (*nptr)=0x903f260 (*nptr)->value=0x903f248 (*nptr)->next=(nil) (*nptr)->value->ptr=0x903f230
Sep 19 00:37:02 appliance mytest[7759]: (*nptr)->value->ptr=0x903f230
Sep 19 00:37:02 appliance mytest[7759]: -- End List Dump --
Sep 19 00:37:02 appliance mytest[7759]: Freeing (*tmp)->value=0x903f208
Sep 19 00:37:02 appliance mytest[7759]: New *nptr value=0x903f260
Sep 19 00:37:02 appliance mytest[7759]: Freeing (*tmp)=0x903f220
Sep 19 00:37:02 appliance mytest[7759]: Freeing (*tmp)->value=0x903f248
Sep 19 00:37:02 appliance mytest[7759]: New *nptr value=(nil)
Sep 19 00:37:02 appliance mytest[7759]: Freeing (*tmp)=0x903f260
在第一次迭代以外的每次迭代中,
tmp
指向上一个节点的下一个
指针-但您已经释放了该节点(在上一次迭代中),因此tmp
指向一个已释放的块,并且您无法解除对它的引用。正如caf所写的,您正在访问刚刚释放的内存
为了解决这个问题,不要使用双指针,单指针在这里会很好
因此,替换
lnode **nptr = &s->head;
借
同样适用于
lnode **tmp = nptr;
在循环中,成功
lnode *tmp = nptr;
当你在做双重作业的时候就把它放下
然后访问值,然后通过
tmp->value
及
直接地我感到困惑;
valgrind
报告引用了一个地址0x0420006C,但是您通过syslog()
进行的跟踪没有引用任何接近该地址的地址。您是否在生成报告后但在生成跟踪之前更改了代码?
lnode *tmp = nptr;
tmp->value
tmp->next