所以我有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)
那是为什么?
答案 0 :(得分:0)
我可以看到addCountry返回newCountry。函数调用后是否要释放它?还要避免强制转换malloc。