Search code examples
cmemory-leaksvalgrind

valgrind definitely lost block calling function


I have a problem figuring out why I get definitely lost error in following code. I have changed return 0 to exit (0) in main function but it does not help.

struct s* function(int K,struct graph *G,int c){
    uint32_t *a;
    a=(uint32_t*)calloc(K+2,sizeof(uint32_t));
    int *b;
    b=(uint32_t*)calloc(K+2,sizeof(uint32_t));
    return s;
}

int main(int argc, char *argv[])
{
    struct S* s=function(K,Gprime,capacity);
    return 0;
}

and Below you can see the S structure code:

struct S{
    int *S;
    uint32_t *Scount;
};

struct S* Sp(int s,int k){
    struct S* sz =malloc(sizeof (struct S));
    sz->S = (int*)calloc(s+1, sizeof(int));
    sz->scount=(uint32_t*)calloc(k+1,sizeof(uint32_t));
    return sz;
}

and here is the valgrind error :

==5343== 680 bytes in 5 blocks are definitely lost in loss record 10 of 52
==5343==    at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==5343==    by 0x40106B: function (reflowk.c:3)
==5343==    by 0x402BB0: main (reflowk.c:9)

Solution

  • You have to call free() to free the memory reserved by malloc and calloc after you have finished using that memory.