即使在内存释放后,valgrind 也会显示内存泄漏 [英] valgrind shows memory leak even after memory free

查看:96
本文介绍了即使在内存释放后,valgrind 也会显示内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 Country.c 文件,其中包含:

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;
}

现在,在程序开始时,我使用addCountry"添加了一些国家

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

对于每个国家,但是当我使用 valgrind 检查内存泄漏时,

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

它表明我确实有来自addCounrty"的内存泄漏,这是日志:

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)

这是为什么?

推荐答案

我可以看到 addCountry 返回了 newCountry.你在函数调用后释放它吗?还要避免强制转换 malloc.

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

这篇关于即使在内存释放后,valgrind 也会显示内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆