简体   繁体   English

` 中的错误<unknown> ': 损坏的双链表: 0x011eb8e0 在调用 free 时偶尔发生</unknown>

[英]Error in `<unknown>': corrupted double-linked list: 0x011eb8e0 happens Occasionally when calling free

I have a problem with free .我有一个free的问题。

I test my code in online compiler but everything is OK.我在在线编译器中测试我的代码,但一切正常。 However in my device (which is a POS) it rises exception when calling free.然而,在我的设备(这是一个 POS)中,它在免费通话时会出现异常。 It frees two of 4 variables and then rise exception.它释放了 4 个变量中的两个,然后引发异常。

My real Code for device:我的真实设备代码:

    char **MenuItems = malloc (MAX_PACKETS * sizeof(char *));

    struct packetInfos * packets = (struct packetInfos *)malloc(sizeof(struct packetInfos)*MAX_PACKETS);      
    size = getPacketNames(groupId,packets,MenuItems);
...
...
...
    TRACE(("====size %d",size));
    for(;i<size;i++) {
        TRACE(("====%d=",i,MenuItems[i]));
        free(MenuItems[i]);
    }

    free(MenuItems);

gives me:给我:

[src/menu.c][NetChargeMenu_L3][1637]>>>====size 4
[src/menu.c][NetChargeMenu_L3][1639]>>>===counter=0  address=34259480
[src/menu.c][NetChargeMenu_L3][1639]>>>===counter=1  address=34260528

Sometimes it frees all variables but give me the following error and freeze everything:有时它会释放所有变量但给我以下错误并冻结所有内容:

Error in `<unknown>': corrupted double-linked list: 0x011eb8e0 

my program is something like this我的程序是这样的

#include <stdio.h>
#include <stdlib.h>

#define MAX 16

int getPacketNames(char **names){
    
    int limit = 4;
    int i = 0;
    
    for(i=0;i<MAX;i++) {
        if(i==limit)
            break;
            
        names[i] = malloc(64);
        sprintf(names[i],"random text %d",i);
    }
    
    return i;
}

int main()
{
    printf("Hello World");
    
    int i=0;
    int size=0;
    
    char **MenuItems = malloc (MAX * sizeof(char *));
    
   size = getPacketNames(MenuItems);
    
    // do something
    
    for(i=0;i<size;i++) {
            
        free(MenuItems[i]);
    }
    
    free(MenuItems);

    
    return 0;
}

I read this post ( What does 'corrupted double-linked list' mean ) by cannot find a useful hint for my code in C.我通过在 C 中找不到我的代码的有用提示来阅读这篇文章( “损坏的双链表”是什么意思)。

Thanks for any hints.感谢您的任何提示。

The cause is for malloc memory not for free it.原因是malloc memory 不是free的。 I used fixed length in allocation.我在分配中使用了固定长度 Before I used dynamic size for allocating, However allocating dynamic size memory must not be a problem.在我使用动态大小进行分配之前,但是分配动态大小 memory 一定不是问题。 but using fixed size allocation maybe solve other problems such my problem.但使用固定大小分配可能会解决其他问题,例如我的问题。

My previous code (With problem when free):我以前的代码(免费时有问题):

struct packetInfos{
    int id;
    char name[64];
    long amount;
};


void getOperatorNames(char *names[]) {

    int i=0;

    for(; i<MAX; i++) {
            sprintf(packets[i].name, "%s", sqlite3_column_text(stmt, 2));
            names[i] = malloc(strlen(packets[i].name) + 1); // the cause of problem in free
            strcpy(names[i], op[i].fname);
    }
}

My current code (Without problem):我当前的代码(没有问题):

void getOperatorNames(char *names[]) {

    int i=0;

    for(; i<MAX; i++) {
            sprintf(packets[i].name, "%s", sqlite3_column_text(stmt, 2));
            names[i] = malloc(64);
            strcpy(names[i], op[i].fname);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM