valgrind shows memory leak even after memory free

坚强是说给别人听的谎言 提交于 2021-01-29 06:30:03

问题


so I have the file Countries.c which contains:

typedef struct City* pCity;
typedef struct Country* pCountry;
typedef struct Territory* pTerritory;
struct City{
    char* name;
    char* food;
    int population;

};

struct Country{
    char *name;
    int numCities;
    pCity cities;
    pTerritory countryTerr;

};

struct Territory{
    int x1;
    int x2;
    int y1;
    int y2;
};
void deleteCountry(pCountry country){
    if(country != NULL){
        int num_of_cities = country->numCities;
        for(int i = 0 ; i<num_of_cities; i++){
            if (country->cities !=NULL){
               if (country->cities[i].food)
                    free(country->cities[i].food);

                if (country->cities[i].name)
                        free(country->cities[i].name);
            }
        }
        if (country->name != NULL){
            free(&(country->name));
        }
        //free(country);
    }
}

pCountry addCountry(char* name,int x1,int y1,int x2,int y2){
    if(name==NULL)
        return NULL;
    pCountry newCountry = NULL;
    newCountry = (pCountry)malloc(sizeof(struct Country));
    if(newCountry==NULL){
        free(newCountry);
        return NULL;
    }
    newCountry->name = (char*)malloc((strlen(name)+1)*sizeof(char));
    newCountry->countryTerr = (pTerritory)malloc(sizeof(struct Territory));
    if(newCountry->name)
        strcpy(newCountry->name,name);
    newCountry->numCities=0;
    if(newCountry->countryTerr){
        newCountry->countryTerr->x1=x1;
        newCountry->countryTerr->y1=y1;
        newCountry->countryTerr->x2=x2;
        newCountry->countryTerr->y2=y2;
    }
    return newCountry;
}


status addCity(pCountry country,pCity city){
    if (country==NULL || city==NULL)
        return failure;

    if(country->numCities==0)
        country->cities = (pCity)malloc(sizeof(struct City));
    else
        country->cities =(pCity)realloc(country->cities,(country->numCities+1)*sizeof(struct City));

    if(!country->cities)
            return failure;

    country->cities[country->numCities] = *city;
    country->numCities++;
    return success;
}

now, In the start of the program, I add some countries using the "addCountry"

function which stored in array of struct pointers.

and then In the end when the user presses to exit, I call to the delete country

for each country, and yet when I checked for memory leak with valgrind,

It shows that I do have memory leak from the "addCounrty", here is the log:

HEAP SUMMARY:
==86249==     in use at exit: 918 bytes in 12 blocks
==86249==   total heap usage: 28 allocs, 16 frees, 7,248 bytes allocated
==86249== 
==86249== Searching for pointers to 12 not-freed blocks
==86249== Checked 68,888 bytes
==86249== 
==86249== 22 bytes in 2 blocks are definitely lost in loss record 1 of 6
==86249==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==86249==    by 0x109150: addCountry (Countries.c:101)
==86249==    by 0x10A310: add_parsed_country (main.c:214)
==86249==    by 0x10A14A: parse_file (main.c:180)
==86249==    by 0x109A3E: main (main.c:17)
==86249== 
==86249== 32 bytes in 2 blocks are definitely lost in loss record 2 of 6
==86249==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==86249==    by 0x109164: addCountry (Countries.c:102)
==86249==    by 0x10A310: add_parsed_country (main.c:214)
==86249==    by 0x10A14A: parse_file (main.c:180)
==86249==    by 0x109A3E: main (main.c:17)
==86249== 
==86249== 96 bytes in 2 blocks are definitely lost in loss record 3 of 6
==86249==    at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==86249==    by 0x109260: addCity (Countries.c:123)
==86249==    by 0x10A3DF: add_parsed_city (main.c:233)
==86249==    by 0x10A1AA: parse_file (main.c:188)
==86249==    by 0x109A3E: main (main.c:17)
==86249== 
==86249== 96 bytes in 4 blocks are definitely lost in loss record 4 of 6
==86249==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==86249==    by 0x10984A: citySetter (Countries.c:210)
==86249==    by 0x10A3C8: add_parsed_city (main.c:232)
==86249==    by 0x10A1AA: parse_file (main.c:188)
==86249==    by 0x109A3E: main (main.c:17)
==86249== 
==86249== 120 bytes in 1 blocks are definitely lost in loss record 5 of 6
==86249==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==86249==    by 0x4EBBB8B: getdelim (iogetdelim.c:62)
==86249==    by 0x10A1C1: parse_file (main.c:176)
==86249==    by 0x109A3E: main (main.c:17)
==86249== 
==86249== LEAK SUMMARY:
==86249==    definitely lost: 366 bytes in 11 blocks
==86249==    indirectly lost: 0 bytes in 0 blocks
==86249==      possibly lost: 0 bytes in 0 blocks
==86249==    still reachable: 552 bytes in 1 blocks
==86249==         suppressed: 0 bytes in 0 blocks
==86249== Reachable blocks (those to which a pointer was found) are not shown.
==86249== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==86249== 
==86249== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
==86249== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

why is that?


回答1:


I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.



来源:https://stackoverflow.com/questions/53459953/valgrind-shows-memory-leak-even-after-memory-free

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!