在我的破坏功能中双倍免费

时间:2013-12-02 05:07:20

标签: c memory-leaks compiler-errors segmentation-fault free

我目前正在尝试从我的程序中解放所有内容并继续获得双重免费问题。我将首先向您展示我的构造函数:

table_t *table_construct (int table_size, int probe_type)
{
    int i;
    table_t *hash;
    if(table_size < 1) return NULL;

    hash = malloc(sizeof(table));
    hash->table = malloc(sizeof(list*) * table_size);
    for(i=0; i < table_size; i++)
    {
        hash->table[i] = (list *)malloc(sizeof(list));
        hash->table[i]->K = 0;
        hash->size++;
    }
    //hash->size = table_size;
    hash->probing_type = probe_type;
    hash->key_entries = 0;
    return hash;
}

我正在创建一个哈希表和malloc一个数组。然后我在程序结束时使用这个破坏函数:

void table_destruct (table *hash)
{
    int i;
    list *list, *temp;

    if(hash == NULL) return;

    for(i=0; i<hash->size; i++)
    {
        list = hash->table[i];
        while(list !=NULL)
        {
            temp = list;
            //list = list->next;
            free(temp);
        }
    }
    free(hash->table);
    free(hash);
} 

我通过数组释放我malloced的所有插槽然后删除整个事情。当我运行它时,我收到此错误:

*** glibc detected *** ./prog: double free or corruption (fasttop): 0x000000000223e2f0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f57f6086b96]
./prog[0x400bbf]
./prog[0x4032d0]
./prog[0x4014fb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f57f602976d]
./prog[0x4009e9]
======= Memory map: ========
00400000-00406000 r-xp 00000000 08:01 272335                          
00606000-00607000 r--p 00006000 08:01 272335                             
00607000-00608000 rw-p 00007000 08:01 272335                             
0223e000-0225f000 rw-p 00000000 00:00 0                                  [heap]
7f57f5df2000-7f57f5e07000 r-xp 00000000 08:01 3411537                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57f5e07000-7f57f6006000 ---p 00015000 08:01 3411537                    /lib/x86_64linux-gnu/libgcc_s.so.1
7f57f6006000-7f57f6007000 r--p 00014000 08:01 3411537                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57f6007000-7f57f6008000 rw-p 00015000 08:01 3411537                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57f6008000-7f57f61bd000 r-xp 00000000 08:01 3417211                    /lib/x86_64-linux-gnu/libc-2.15.so
7f57f61bd000-7f57f63bc000 ---p 001b5000 08:01 3417211                    /lib/x86_64-linux-gnu/libc-2.15.so
7f57f63bc000-7f57f63c0000 r--p 001b4000 08:01 3417211                    /lib/x86_64-linux-gnu/libc-2.15.so
7f57f63c0000-7f57f63c2000 rw-p 001b8000 08:01 3417211                    /lib/x86_64-linux-gnu/libc-2.15.so
7f57f63c2000-7f57f63c7000 rw-p 00000000 00:00 0 
7f57f63c7000-7f57f64c2000 r-xp 00000000 08:01 3417221                    /lib/x86_64-linux-gnu/libm-2.15.so
7f57f64c2000-7f57f66c1000 ---p 000fb000 08:01 3417221                    /lib/x86_64-linux-gnu/libm-2.15.so
7f57f66c1000-7f57f66c2000 r--p 000fa000 08:01 3417221                    /lib/x86_64-linux-gnu/libm-2.15.so
7f57f66c2000-7f57f66c3000 rw-p 000fb000 08:01 3417221                    /lib/x86_64-linux-gnu/libm-2.15.so
7f57f66c3000-7f57f66e5000 r-xp 00000000 08:01 3417227                    /lib/x86_64-linux-gnu/ld-2.15.so
7f57f68cf000-7f57f68d2000 rw-p 00000000 00:00 0 
7f57f68e1000-7f57f68e5000 rw-p 00000000 00:00 0 
7f57f68e5000-7f57f68e6000 r--p 00022000 08:01 3417227                    /lib/x86_64-linux-gnu/ld-2.15.so
7f57f68e6000-7f57f68e8000 rw-p 00023000 08:01 3417227                    /lib/x86_64-linux-gnu/ld-2.15.so
7fff79fe9000-7fff7a00a000 rw-p 00000000 00:00 0                          [stack]
7fff7a0e6000-7fff7a0e7000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

我无法找出问题所在。我还没有看到任何东西?

2 个答案:

答案 0 :(得分:2)

Destruct只需要:

void table_destruct (table_t *hash)
{
    int i;

    if (hash == NULL) 
       return;

    for (i=0; i<hash->size; i++)
    {
        free(hash->table[i];);
    }
    free(hash->table);
    free(hash);
} 

答案 1 :(得分:0)

for(i=0; i<hash->size; i++)
{
    list = hash->table[i];
    while(list !=NULL)
    {
        temp = list;
        //list = list->next;
        free(temp);
    }
}

您永远不会分配list = NULL,因此,while条件仍为true,并且它再次进入循环free list,您只有{ {1}}。