valgrind shows memory leak even after memory free
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?
c pointers memory-leaks valgrind
add a comment |
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?
c pointers memory-leaks valgrind
Your callfree(&(country->name))is wrong and leads to undefined behavior.
– Some programmer dude
Nov 24 '18 at 16:12
Good catch, @Someprogrammerdude, but inasmuch asnameis the first member ofstruct Country, it is rather the case thatfree(&(country->name))is equivalent tofree(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.
– John Bollinger
Nov 24 '18 at 16:27
1
I observe, too, that there is a commented-outfree(country)in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the otherfreewould have silenced such an error, but for the wrong reason.
– John Bollinger
Nov 24 '18 at 16:30
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39
add a comment |
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?
c pointers memory-leaks valgrind
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?
c pointers memory-leaks valgrind
c pointers memory-leaks valgrind
edited Nov 24 '18 at 16:09
ks1322
22.4k968111
22.4k968111
asked Nov 24 '18 at 16:05
kal polakal pola
111
111
Your callfree(&(country->name))is wrong and leads to undefined behavior.
– Some programmer dude
Nov 24 '18 at 16:12
Good catch, @Someprogrammerdude, but inasmuch asnameis the first member ofstruct Country, it is rather the case thatfree(&(country->name))is equivalent tofree(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.
– John Bollinger
Nov 24 '18 at 16:27
1
I observe, too, that there is a commented-outfree(country)in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the otherfreewould have silenced such an error, but for the wrong reason.
– John Bollinger
Nov 24 '18 at 16:30
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39
add a comment |
Your callfree(&(country->name))is wrong and leads to undefined behavior.
– Some programmer dude
Nov 24 '18 at 16:12
Good catch, @Someprogrammerdude, but inasmuch asnameis the first member ofstruct Country, it is rather the case thatfree(&(country->name))is equivalent tofree(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.
– John Bollinger
Nov 24 '18 at 16:27
1
I observe, too, that there is a commented-outfree(country)in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the otherfreewould have silenced such an error, but for the wrong reason.
– John Bollinger
Nov 24 '18 at 16:30
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39
Your call
free(&(country->name)) is wrong and leads to undefined behavior.– Some programmer dude
Nov 24 '18 at 16:12
Your call
free(&(country->name)) is wrong and leads to undefined behavior.– Some programmer dude
Nov 24 '18 at 16:12
Good catch, @Someprogrammerdude, but inasmuch as
name is the first member of struct Country, it is rather the case that free(&(country->name)) is equivalent to free(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.– John Bollinger
Nov 24 '18 at 16:27
Good catch, @Someprogrammerdude, but inasmuch as
name is the first member of struct Country, it is rather the case that free(&(country->name)) is equivalent to free(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.– John Bollinger
Nov 24 '18 at 16:27
1
1
I observe, too, that there is a commented-out
free(country) in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the other free would have silenced such an error, but for the wrong reason.– John Bollinger
Nov 24 '18 at 16:30
I observe, too, that there is a commented-out
free(country) in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the other free would have silenced such an error, but for the wrong reason.– John Bollinger
Nov 24 '18 at 16:30
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39
add a comment |
1 Answer
1
active
oldest
votes
I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459953%2fvalgrind-shows-memory-leak-even-after-memory-free%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.
add a comment |
I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.
add a comment |
I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.
I can see that addCountry returns newCountry. Are you freeing it after function call? Also avoid casting malloc.
answered Nov 24 '18 at 16:53
ShadowchaserShadowchaser
864
864
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459953%2fvalgrind-shows-memory-leak-even-after-memory-free%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your call
free(&(country->name))is wrong and leads to undefined behavior.– Some programmer dude
Nov 24 '18 at 16:12
Good catch, @Someprogrammerdude, but inasmuch as
nameis the first member ofstruct Country, it is rather the case thatfree(&(country->name))is equivalent tofree(country). That's surely not what the OP intended, and it likely explains at least some of the failures to free, but in itself it is not wrong and does not produce UB.– John Bollinger
Nov 24 '18 at 16:27
1
I observe, too, that there is a commented-out
free(country)in the OP's code. Since the OP is analyzing this code with Valgrind, I'd be inclined to speculate that that arises from the OP peviously correcting a Valgrind multiple-free error arising from the same underlying mistake. Commenting out the otherfreewould have silenced such an error, but for the wrong reason.– John Bollinger
Nov 24 '18 at 16:30
I think deleteCountry should also free the countryTerr and cities fields of the country
– dmuir
Nov 24 '18 at 16:39