C语言 *glibc 检测到双重释放或损坏() * 消息!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4063583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:51:56  来源:igfitidea点击:

*glibc detected double free or corruption() * message!

cfree

提问by FILIaS

The following deleteNode function when I run the program gets these: * glibc detected free(): invalid next size (normal): 0x000000000103dd90 **

当我运行程序时,下面的 deleteNode 函数得到这些: * glibc 检测free():下一个尺寸无效(正常):0x000000000103dd90 **

Even i make the ' free(here); ' a comment,i get the above message. I dont think that the other 'free' calls provokes a problem like that. But I cant see why this would be wrong. :/

即使我让 ' 免费(这里); ' 一条评论,我收到了上述消息。我不认为其他“免费”电话会引发这样的问题。但我不明白为什么这会是错误的。:/

struct List *deleteNode(int Code,int i,char* Number)
    {
        struct List *here;
        here=Head;

        for (here; here!=Tail; here=here->next)
        {       
            if ( (here->number==Number) && (here->code==Code) )//found node on the List
            {
                if (here->previous==Head)        //delete from beginning
                {           
                    Head=here->next;
                    here->next->previous=Head;
                }
                else if (here->next==Tail) //delete from the end
                {
                    here->previous->next=Tail;
                    Tail=here->previous;
                }
                else  //delete from the middle of the list
                {   
                    here->previous->next=here->next;
                    here->next->previous=here->previous;
                }
                break;
            }
        }

        free (here);

    }

EDIT: if i used and understand valgring well then the problem is on my main function. i have also there some 'free' but i changed deleteNode before this message so i thought that the problem was on the deleteNode function.

编辑:如果我很好地使用并理解了 valgring,那么问题就出在我的主要功能上。我也有一些“免费”,但我在此消息之前更改了 deleteNode,所以我认为问题出在 deleteNode 函数上。

Now,there is no free() invalid next size.... but unfortunately this: glibc detected * : double free or corruption (out): 0x00007fff1aae9ae0 *:(

现在,没有 free() 无效的下一个大小......但不幸的是:glibc 检测到*:双重释放或损坏(输出):0x00007fff1aae9ae0 *:(

A part of the main:

主要的一部分:

FILE *File;
    if ( ( File=fopen("File.txt","r")) !=NULL )
    {                               
        int li = 0;    
        char *lin = (char *) malloc(MAX_LINE * sizeof(char));


        while(fgets(lin, MAX_LINE, eventFile) != NULL)
        {
            token = linetok(lin, " ");

            if(token != NULL)
            {

                int i,code,nodeID;
            char *number;
            char *event;

                for(i = 0; token[i] != NULL; i += 1)
                {
            code=atoi(token[0]);
            strcpy(event,token[1]);
            nodeID=atoi(token[2]);
            strcpy(number,token[3]) ;

            int i;
            if (!strcmp(event,"add"))
            {       
                add_to_List(code,i,number);
            }
            else if(!strcmp(event,"delete"))
            {       
                             deleteNode(eventNo,i,number);
                    }
            free(event);
            free(phoneNumber);  
        }
                free(token);
            }
            else 
            {
                printf("Error reading line %s\n", lin);
                exit(1);   
            }
        }
    } 
    else 
    {
        printf("Error opening file with the events.\nEXIT!");
        exit(0);
    }

debugging it...

调试它...

multiple definition of main' pro:(.text+0xce0): first defined here /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition ofDTOR_END' pro:(.dtors+0x8): first defined here /usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. /usr/bin/ld: error in pro1(.eh_frame); no .eh_frame_hdr table will be created. collect2: ld returned 1 exit status

main' pro:(.text+0xce0): first defined here /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition ofDTOR_END' pro:(.dtors+0x8) 的多重定义:首先在这里定义 /usr/bin/ld: 警告:无法创建 .eh_frame_hdr 部分,--eh-frame-hdr 被忽略。/usr/bin/ld: pro1(.eh_frame) 中的错误;不会创建 .eh_frame_hdr 表。collect2: ld 返回 1 个退出状态

回答by paxdiablo

"Invalid next size" means that glibc has detected corruption in your memory arena.

“无效的下一个大小”意味着 glibc 已检测到您的内存领域中的损坏。

You have overwritten valuable accounting information that's stored in between your allocated blocks.

您已经覆盖了存储在分配块之间的有价值的会计信息。

With each block that mallocgives you, there is some accounting information stored close by. When you overwrite this information by, for example, writing 128 characters to a 20-character buffer, glibc may detect this the next time you try to free (or possibly allocate) some memory.

对于malloc给您的每个块,都有一些会计信息存储在附近。例如,当您通过将 128 个字符写入 20 个字符的缓冲区来覆盖此信息时,glibc 可能会在您下次尝试释放(或可能分配)一些内存时检测到这一点。

You need to find the root cause of this problem - it's not the free itself, that's just where the problem is being detected. Somewhere, some of your code is trashing memory and a memory analysis tool like valgrind will be invaluable here.

您需要找到此问题的根本原因 - 不是免费本身,而是检测到问题的地方。在某个地方,你的一些代码正在破坏内存,像 valgrind 这样的内存分析工具在这里将是无价的。

回答by sth

If the node is not found in the list, you will free the Tailnode at the end of the function, without updating Tailto point to anything valid again.

如果在列表中未找到该节点,您将Tail在函数结束时释放该节点,而无需更新Tail以再次指向任何有效内容。

Further using the list and the now deallocated Tailcan easily result in memory corruption that might later be detected by glibc with a message like the one you got.

进一步使用列表和现在释放的内容Tail很容易导致内存损坏,glibc 稍后可能会检测到该消息,并显示您收到的消息。

Also note that in (here->number==Number)you are comparing two pointers, not the values those pointers point to. I'm not sure if that's what you want.

另请注意,(here->number==Number)您正在比较两个指针,而不是这些指针指向的值。我不确定这是否是你想要的。