c - valgrind 即使在内存释放后也显示内存泄漏

标签 c pointers memory-leaks valgrind

所以我有文件Countrys.c,其中包含:

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”添加一些国家/地区

存储在结构指针数组中的函数。

然后最后当用户按下退出时,我调用删除国家

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

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

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。

关于c - valgrind 即使在内存释放后也显示内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459953/

相关文章:

c++ - C++中的按位设置

c - 从直方图中寻找众数

C++ 函数 : ampersand vs asterisk

ios - 启用 ARC 的 AFNetworking 2 后异步内存泄漏

c++ - 使用 x 关闭程序时内存泄漏

c - 从排序链表中一次性删除 "2 duplicated"个元素

c - 从 C 中的帧中提取错误的数据?

c - C 中指向指针的指针?

c++ - 堆栈链表中的弹出和推送函数

ios - 了解我的 iPhone 应用程序崩溃日志