简体   繁体   中英

Leak errors in valgrind

I used in valgrind with my program and I got 2 errors :

==4909==     in use at exit: 80 bytes in 3 blocks
==4909==   total heap usage: 41 allocs, 38 frees, 8,401 bytes allocated
==4909== 
==4909== 32 bytes in 2 blocks are definitely lost in loss record 1 of 2
==4909==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4909==    by 0x109CCE: addChar (Jerry.c:216)
==4909==    by 0x10AD57: main (MainFirst.c:128)
==4909== 
==4909== 48 bytes in 1 blocks are definitely lost in loss record 2 of 2
==4909==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4909==    by 0x109D98: addChar (Jerry.c:226)
==4909==    by 0x10AD57: main (MainFirst.c:128)
==4909== 
==4909== LEAK SUMMARY:
==4909==    definitely lost: 80 bytes in 3 blocks
==4909==    indirectly lost: 0 bytes in 0 blocks
==4909==      possibly lost: 0 bytes in 0 blocks
==4909==    still reachable: 0 bytes in 0 blocks
==4909==         suppressed: 0 bytes in 0 blocks
==4909== 
==4909== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

The errors point to one function:

  • The first in line 215 is the malloc() service;
  • The second in line 225 is the realloc() service.
    int i=0;
    for (;i<countJerry;i++){
        if (strcmp(jerries[i].jerryID,j.jerryID)==0)
            break;
    }
    if (jerries[i].numOfChar==0)
    {
        jerries[i].jerryChar=(PhysicalCharacteristics*)malloc(sizeof(PhysicalCharacteristics));
        if (jerries[i].jerryChar==NULL)
        {
            printf("Memory Problem");
            freeMalloc();
            exit(1);
        }
    }
    else
    {
        jerries[i].jerryChar=(PhysicalCharacteristics*)realloc(jerries[i].jerryChar,(jerries[i].numOfChar+1)*sizeof(PhysicalCharacteristics));
        if (jerries[i].jerryChar==NULL)
        {
            printf("Memory Problem");
            freeMalloc();
            exit(1);
        }
    }
    jerries[i].jerryChar[jerries[i].numOfChar]=character;
    jerries[i].numOfChar++;
}

I can't find the problem. The original code is too long and I free all the mallocs/reallocs I created.


                    for (i=countJerry-1;i>=0;i--)
                    {
                    for (j=jerries[i].numOfChar-1;j>=0;j--)
                        free(jerries[i].jerryChar[j].nameChar);
                    free(jerries[i].jerryID);
                //  free(jerries[i].jerryChar);
                    }
                    free(jerries);
                    free(character);
                    for (i=countOrigins-1;i>=0;i--)
                        free(origins[i].nameO);
                    free(origins);
                    for(i=countPlanets-1;i>=0;i--)
                        free(planets[i].nameP);
                    free(planets);
                

So I can't find the problem.

Be careful, when you use realloc() . When it returns NULL (ie when the free memory is exhausted), the original buffer is not freed. If you use the pointer on the original buffer, you loose the address of the buffer when realloc() returns NULL. The manual says:

If realloc() fails, the original block is left untouched; it is not freed or moved.

So, it is preferable to use a temporary pointer to get the result of realloc() :

PhysicalCharacteristics *tmp;
tmp=(PhysicalCharacteristics*)realloc(jerries[i].jerryChar, (jerries[i].numOfChar+1)*sizeof(PhysicalCharacteristics));
if (tmp==NULL) {
  printf("Memory Problem");
  freeMalloc();
  exit(1);
}
jerries[i].jerryChar = tmp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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