Hey I'm doing a project about Olympic database that keeps record of medals. Now I have a problem with freeing memory (Valgrind error) and I have no clue how I should free the memory?
Valgrind error comes from the lines:
data[i].country = malloc(strlen(str) + 2);
add_country(countrydata, countryname, i);
First function tries to add country names to the database
    typedef struct Olympia
    {
        char* country;
        int gold;
        int silver;
        int bronze;
    }Olympia;
int add_country(struct Olympia* data, char* str, int i)
    {   
        if (str[0] == '\0') //checking that input is correct
        {
            printf("Error! Try again!\n");
        }
        else
        {
            data[i].country = malloc(strlen(str) + 2);  //allocating memory for country name
            strcpy(data[i].country, str);   //adding country to database
            data[i].gold = 0;   //setting medals to zero
            data[i].silver = 0;
            data[i].bronze = 0;
            i++;
            printf("Country added to database succesfully!\n");
        }
        return i;
    }
Then there is the main function
    int main(void)
    {
        char command;
        int gold = 0;
        int silver = 0;
        int bronze = 0;
        int i = 0;
        char filename[100];
        char* line = (char*)malloc((100) * sizeof(char)); //allocating memory for one stdin line
        char* countryname = (char*)malloc(20 * sizeof(char)); // allocating memory for country name
        struct Olympia* countrydata = malloc(sizeof(struct Olympia) * 1); //allocating memory for structure
        while(1)
        {
            fgets(line, 100, stdin); //reading one line of stdin
            if (feof(stdin) != 0)
            {
                printf("File processing completed!\n");
                free(line);
                free(countryname);
                free(countrydata);
                return 0;
            }
            switch (line[0]) //finding the right command
            {
                case 'A':
                    if (sscanf(line, "%c %s", &command, countryname) == 2)
                    {
                        add_country(countrydata, countryname, i);
                        i++;
                        countrydata = realloc(countrydata, sizeof(struct Olympia) * (i + 1));
                    }
                    else
                    {
                        printf("Error! Invalid input, try again!");
                    }
                    break;
            case 'Q':
                free(line);
                free(countryname);
                free(countrydata);
                return(EXIT_SUCCESS);
            default:
                printf("Error! Invalid input.\n");              
        }   
    }
}
 
     
    