-1

My program crached on Eclipse when i try to free my object - PokemonTrainer.I have tried the solution in this article, but it didn't help.

PokemonTrainer pokemonTrainerCreate(char* name, Pokemon initial_pokemon,
    int max_num_local, int max_num_remote)
{
    PokemonTrainer trainer = malloc(sizeof(PokemonTrainer));

    if ((name == NULL) || (initial_pokemon == NULL) || (trainer == NULL) ||
        (max_num_local < 0) || (max_num_remote < 0))
        return NULL;

    char tmp_name[strlen(name)];
    strcpy(tmp_name, name);
    trainer->name = tmp_name;
    trainer->max_num_local = max_num_local;
    trainer->max_num_remote = max_num_remote;
    trainer->pokemons_local = malloc(sizeof(Pokemon)
        trainer->max_num_local);
    trainer->pokemons_remote = malloc(sizeof(Pokemon)
        trainer->max_num_remote);

    if (trainer->pokemons_remote == NULL) {
        free(trainer->pokemons_local);
        return NULL;
    } else if (trainer->pokemons_local == NULL) {
        free(trainer->pokemons_remote);
        return NULL;
    }

    trainer->pokemons_local[0] = pokemonCopy(initial_pokemon);
    trainer->curr_num_local = 1;
    trainer->curr_num_remote = 0;

    return trainer;
}

void pokemonTrainerDestroy(PokemonTrainer trainer)
{
    if (trainer == NULL)
        return;

    if (trainer->curr_num_local > 0)
        for (int i = trainer->curr_num_local - 1; i >= 0; i--)
            pokemonDestroy(trainer->pokemons_local[i]);

    if (trainer->curr_num_remote > 0)
        for (int i = trainer->curr_num_remote - 1; i >= 0; i--)
            pokemonDestroy(trainer->pokemons_remote[i]);

    free (trainer);  // here it's crashed
}

It is during the execution of free() in the stack that I am getting a "No source available for "ntdll!RtlpNtEnumerateSubKey() at 0x77cf04e5" error.

Community
  • 1
  • 1
user123456
  • 21
  • 1
  • 1
    `char tmp_name[strlen(name)];` --> `char tmp_name[strlen(name)+1];` but `trainer->name = tmp_name;` is invalid outside this function. – BLUEPIXY Dec 05 '16 at 20:11
  • 1
    `trainer->pokemons_local = malloc(sizeof(Pokemon) trainer->max_num_local);` does that compile? isn't a `*` missing? – Jean-François Fabre Dec 05 '16 at 20:13

1 Answers1

1

PokemonTrainer trainer = malloc(sizeof(PokemonTrainer)); is unlikely to work properly since you're allocating the size of the pointer, not the real data.

You won't have enough storage => undefined behaviour happens, and for you it happens when freeing the memory (corrupt memory list)

I would do this:

 PokemonTrainer trainer = malloc(sizeof(*PokemonTrainer));

so the sizeof takes the size of the structure pointed by PokemonTrainer.

EDIT: for completeness, BLUEPIXY suggests that you've missing 1 byte here (because of null-termination char):

char tmp_name[strlen(name)]; 
strcpy(tmp_name, name);

and moreover this allocated space is temporary, so I'd suggest:

char *tmp_name = strdup(name);

which will allocate the correct size and performs a dynamic allocation that stays valid even after returning from the routine.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219