c - Memory leak during insert -


writing arraytable implementation , getting memory leak warning valgrind during insert function.

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 = min_size; < max_size; i++){             if(!array_hasvalue(tmptable->values,i)){                 array_setvalue(tmptable->values,tmpelement,i);                 //quit loop                 = max_size;                 tmptable->noelements++;             }         }     } }//end table insert==20201== 

i have tried free memory in case wasn't passed through if-statements still no luck. got suggestions on i'm doing wrong?

edit valgrind output:

==20201== ==20201== heap summary: ==20201==     in use @ 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 lost in loss record 1 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x4017e9: correctnesstest (tabletest.c:176) ==20201==    0x4024a4: testing (tabletest.c:396) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== 4 bytes in 1 blocks lost in loss record 2 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x4017f6: correctnesstest (tabletest.c:176) ==20201==    0x4024a4: testing (tabletest.c:396) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== 40 bytes in 10 blocks lost in loss record 3 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x401f5d: speedtest (tabletest.c:299) ==20201==    0x4024e4: testing (tabletest.c:404) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== 40 bytes in 10 blocks lost in loss record 4 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x401f7d: speedtest (tabletest.c:299) ==20201==    0x4024e4: testing (tabletest.c:404) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== 68 bytes in 17 blocks lost in loss record 5 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x401b30: correctnesstest (tabletest.c:226) ==20201==    0x4024a4: testing (tabletest.c:396) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== 68 bytes in 17 blocks lost in loss record 6 of 6 ==20201==    @ 0x4c28bed: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20201==    0x401498: intptrfromint (tabletest.c:51) ==20201==    0x401b50: correctnesstest (tabletest.c:226) ==20201==    0x4024a4: testing (tabletest.c:396) ==20201==    0x402500: main (tabletest.c:410) ==20201== ==20201== leak summary: ==20201==    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== counts of detected , suppressed errors, rerun with: -v ==20201== error summary: 6 errors 6 contexts (suppressed: 4 4) 

function @ lines using intptrtoint

 table_insert(table,intptrfromint(keys[i]), intptrfromint(values[i]));    int *intptrfromint(int i){         int *ip=malloc(sizeof(int));         *ip=i;         return ip; } 

edit 2:

sorry guys, way off. remove function wrong , didn't free memory:

void table_remove(table *table, key key){     arraytable *tmptable = (arraytable*)table;     arrayelement *tmpelement;     for(int = min_size; < 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 

but many frees , invalid free valgrid


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -