Valgrind的...大小8 free'd的块中的4个字节 [英] Valgrind... 4 bytes inside a block of size 8 free'd

查看:229
本文介绍了Valgrind的...大小8 free'd的块中的4个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图释放一个列表之后获得在Valgrind的这个错误。 print_list转储清单到系统日志。我是pretty确信输出是正确的。

Valgrind的:

  == == 7028在10范围内1错误:
== == 7028大小为4的读取无效
== == 7028在0x8049603:free_list(list.c:239)
== == 7028通过0x80488B5:m61_close_for_valgrind(m61.c:36)
== == 7028通过0x8048825:主(mytest.c:19)
== == 7028地址0x420006c是大小8 free'd的块中的4个字节
== == 7028在0x4028F0F:免费(vg_replace_malloc.c:446)
== == 7028通过0x804960C:free_list(list.c:239)
== == 7028通过0x80488B5:m61_close_for_valgrind(m61.c:36)
== == 7028通过0x8048825:主(mytest.c:19)
== == 7028

mytest.c:

  15的char * TEMP =的malloc(10);
16字符* TEMP2 =的malloc(10);
17免费(TEMP);
18免费(TEMP2);
19 m61_close_for_valgrind();

list.h

  typedef结构叶子节点{
    ACTIVE_ALLOCATION *值;
    结构叶子节点*接下来的;
}叶子节点;

list.c(由m61_close_for_valgrind称为()

 无效free_list(LIST * S){    叶子节点** NPTR =安培; S->头;    print_list(多个);
    而(* NPTR){
        叶子节点** TMP = NPTR;
        TMP = NPTR;        如果((* TMP) - GT;的值){
            系统日志(LOG_NOTICE,舷(* TMP) - GT;值=%P \\ N,(* TMP) - GT;值);
            //的printf(%P \\ N,(* NPTR) - GT;值);
            免费((* TMP) - GT;值); //自由积极配置元数据
        }        NPTR =及(* NPTR) - >接着,
        系统日志(LOG_NOTICE,新* NPTR值=%P \\ N,(* NPTR));        系统日志(LOG_NOTICE,舷(* TMP)=%P \\ N,(* TMP));
        免费(* TMP); //自由节点    }}

日志

  09月19日零时37分02秒家电mytest的[7759]: - 启动列表转储 - 
9月19日0点37分02秒家电mytest的[7759]:(* NPTR)= 0x903f220(* NPTR) - GT;值= 0x903f208(* NPTR) - >接下来= 0x903f260(* NPTR) - GT;&值 - GT; PTR = 0x903f1f0
9月19日0点37分02秒家电mytest的[7759]:(* NPTR) - GT;&值 - GT; PTR = 0x903f1f0
9月19日0点37分02秒家电mytest的[7759]:(* NPTR)= 0x903f260(* NPTR) - GT;值= 0x903f248(* NPTR) - >接下来=(无)(* NPTR) - GT;值 - > PTR = 0x903f230
9月19日0点37分02秒家电mytest的[7759]:(* NPTR) - GT;&值 - GT; PTR = 0x903f230
9月19日0点37分02秒家电mytest的[7759]: - 结束目录自卸 -
9月19日0点37分02秒家电mytest的[7759]:释放(* TMP) - GT;值= 0x903f208
9月19日0点37分02秒家电mytest的[7759]:新* NPTR值= 0x903f260
9月19日0点37分02秒家电mytest的[7759]:释放(* TMP)= 0x903f220
9月19日0点37分02秒家电mytest的[7759]:释放(* TMP) - GT;值= 0x903f248
9月19日0点37分02秒家电mytest的[7759]:新* NPTR值=(无)
9月19日0点37分02秒家电mytest的[7759]:释放(* TMP)= 0x903f260


解决方案

由于非洲足联已经写,你所访问刚刚被释放的内存。

要解决这个问题,只是不使用双指针,指针单会做非常好这里。

所以,替换

 叶子节点** NPTR =安培; S->头;

 叶子节点* NPTR = S->头;

同为

 叶子节点** TMP = NPTR;

中循环。让它

 叶子节点* TMP = NPTR;

和下降只是当你在它的双重任务。

然后访问值和明年由

  tmp->价值

接下来,

  tmp-&GT

直接

I'm getting this error in Valgrind after attempting to free a list. print_list dumps the list to the syslog. I'm pretty confident that output is correct.

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==    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();

list.h

typedef struct lnode {
    ACTIVE_ALLOCATION *value;
    struct lnode *next;
} lnode;

list.c (Called by m61_close_for_valgrind()

void free_list(LIST *s) {

    lnode **nptr = &s->head;

    print_list(s);
    while (*nptr) {
        lnode **tmp = nptr;
        tmp = nptr;

        if ((*tmp)->value) {
            syslog(LOG_NOTICE,"Freeing (*tmp)->value=%p\n", (*tmp)->value);
            //printf("%p\n",(*nptr)->value);
            free((*tmp)->value);    //Free active allocation metadata
        }

        nptr = &(*nptr)->next;
        syslog(LOG_NOTICE,"New *nptr value=%p\n", (*nptr));

        syslog(LOG_NOTICE,"Freeing (*tmp)=%p\n", (*tmp));
        free(*tmp);             //Free node

    }

}

syslog

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

解决方案

As caf already wrote, you're accessing memory that has just been freed.

To fix that, just don't use double pointers, single pointers will do very well here.

So replace

lnode **nptr = &s->head;

by

lnode *nptr = s->head;

Same for

lnode **tmp = nptr;

in the loop. Make it

lnode *tmp = nptr;

and drop the double assignment just when you at it.

Then access value and next by

tmp->value

and

tmp->next

directly

这篇关于Valgrind的...大小8 free'd的块中的4个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆