插入期间内存泄漏

时间:2014-03-24 12:49:59

标签: c memory-leaks

在插入函数期间编写此数组表实现并从valgrind获取一些内存泄漏警告。

void table_insert(Table *table, KEY key, VALUE value){
    ArrayTable *tmpTable = (ArrayTable*)table;
    ArrayElement *tmpElement = malloc(sizeof(ArrayElement));
    tmpElement->key = key;
    tmpElement->value = value;
    //hitta nästa tomma index
    if(tmpTable->noElements < MAX_SIZE){
        for(int i = MIN_SIZE; i < MAX_SIZE; i++){
            if(!array_hasValue(tmpTable->values,i)){
                array_setValue(tmpTable->values,tmpElement,i);
                //Quit the loop
                i = MAX_SIZE;
                tmpTable->noElements++;
            }
        }
    }
}//end table insert==20201==

我试图释放这个记忆,以防它没有通过if语句,但仍然没有运气。有人对我做错了什么有任何建议吗?

编辑valgrind输出:

==20201==
==20201== HEAP SUMMARY:
==20201==     in use at exit: 224 bytes in 56 blocks
==20201==   total heap usage: 108 allocs, 52 frees, 161,314 bytes allocated
==20201==
==20201== 4 bytes in 1 blocks are definitely lost in loss record 1 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x4017E9: correctnessTest (tabletest.c:176)
==20201==    by 0x4024A4: testing (tabletest.c:396)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== 4 bytes in 1 blocks are definitely lost in loss record 2 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x4017F6: correctnessTest (tabletest.c:176)
==20201==    by 0x4024A4: testing (tabletest.c:396)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== 40 bytes in 10 blocks are definitely lost in loss record 3 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x401F5D: speedTest (tabletest.c:299)
==20201==    by 0x4024E4: testing (tabletest.c:404)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== 40 bytes in 10 blocks are definitely lost in loss record 4 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x401F7D: speedTest (tabletest.c:299)
==20201==    by 0x4024E4: testing (tabletest.c:404)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== 68 bytes in 17 blocks are definitely lost in loss record 5 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x401B30: correctnessTest (tabletest.c:226)
==20201==    by 0x4024A4: testing (tabletest.c:396)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== 68 bytes in 17 blocks are definitely lost in loss record 6 of 6
==20201==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20201==    by 0x401498: intPtrFromInt (tabletest.c:51)
==20201==    by 0x401B50: correctnessTest (tabletest.c:226)
==20201==    by 0x4024A4: testing (tabletest.c:396)
==20201==    by 0x402500: main (tabletest.c:410)
==20201==
==20201== LEAK SUMMARY:
==20201==    definitely lost: 224 bytes in 56 blocks
==20201==    indirectly lost: 0 bytes in 0 blocks
==20201==      possibly lost: 0 bytes in 0 blocks
==20201==    still reachable: 0 bytes in 0 blocks
==20201==         suppressed: 0 bytes in 0 blocks
==20201==
==20201== For counts of detected and suppressed errors, rerun with: -v
==20201== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 4 from 4)

使用intPtrToInt

在这些行上的函数
 table_insert(table,intPtrFromInt(keys[i]), intPtrFromInt(values[i]));


 int *intPtrFromInt(int i){
        int *ip=malloc(sizeof(int));
        *ip=i;
        return ip;
}

编辑2:

对不起伙计们,我离开了。删除功能是错误的,并没有释放内存:

void table_remove(Table *table, KEY key){
    ArrayTable *tmpTable = (ArrayTable*)table;
    ArrayElement *tmpElement;
    for(int i = MIN_SIZE; i < MAX_SIZE; i++){
        if(array_hasValue(tmpTable->values,i)){
            tmpElement = array_inspectValue(tmpTable->values,i);
            if(table_lookup(tmpTable,key) != NULL){
                free(tmpElement->key);
                free(tmpElement->value);
                array_setValue(tmpTable->values,NULL,i);
                tmpTable->noElements--;
                //i = MAX_SIZE;
            }
        }
    }
}//end remove

但是现在我获得了太多的免费和无效的 valgrid

0 个答案:

没有答案