Access violation error when trying to free an array

0

I'm currently working on structures for a personnal project. I'm trying to use dynamically-allocated bidimensionnal arrays, and then free the space they're taking. An error occurs when I try to free the allocated space.

I was able to narrow down the source of the issue down to the function I had set up to destroy my structures, but wasn't able to pinpoint the cause of the error within. More concerning is the fact that the error, an access violation, only triggers half of the time. I've included the functions below, with a few comments. I'm sorry if I'm including too much code here, but I'm geniunely lost at words and do feel like the way I initialize the structures may have an impact on whether I'm destroying them correctly.

#include <stdio.h>
#include <stdlib.h>

typedef struct f_part f_part;
typedef struct field field;

//these are the structures I used:
//part of a field: includes a character and a color
struct f_part
{
    char caractere;
    short int color;
};


//field: points to a bidimensionnal array of f_part, and remember its size
struct field
{
    int fsize_x;
    int fsize_y;
    f_part **fbody;
};

field* fieldInitialize(const int size_x, const int size_y)  //this function seems to work correctly, I've mostly added it as an indicator
{
    field* terrain = malloc(sizeof(*terrain));
    if (terrain == NULL)
        printf("fail1");
    terrain->fsize_x = size_x;
    terrain->fsize_y = size_y;
    f_part* ptrToFPart = NULL;
    terrain->fbody = malloc(sizeof(ptrToFPart) * size_x);   //this is where I allocate the space for an array of pointers
    if (terrain->fbody == NULL)
        printf("fail2");

    int i,j;
    for (i = 0 ; i < size_x ; i++)
    {
        terrain->fbody[i] = malloc(sizeof(f_part) * size_y);
        for (j = 0 ; j < size_y ; j++)
        {
            terrain->fbody[i][j].caractere = 'a';
            terrain->fbody[i][j].color = 0;
        }
    }
    terrain->fsize_x = size_x;
    terrain->fsize_y = size_y;
    return terrain;
}



void fieldDestroy(field* terrain)   //this is the function that is supposed to destroy the object and free the memory, and fails
{
    int i;
    for (i = 0 ; i < terrain->fsize_x ; i++)
    {
        free(terrain->fbody[i]);    //this part always goes well
    }
    printf("flag 1\n");
    free(terrain->fbody);   //this is where the access violation happens, when it does
    printf("flag 2\n");
    free(terrain);      //this part goes well too
    printf("flag 3\n");
}

int main()
{
    field* testField = fieldInitialize(5, 5);
    fieldDestroy(testField);       //This is the function that fails. Sometimes.
return 0;
}

The error systematically happens on that second-to-last line, when I try to free the space allocated for the array of pointers. Except it doesn't always happen! Sometimes, I can free terrain->fbody, and everything goes well, but other times, I can't free the space. The error I get is 0xC0000005, which apparently translates to "access violation". Which I understand would be common when dealing with dynamic memory allocation, but then why do I seem to get the error only half of the time?

Edit : Okay, so I've edited a bit of the code. Interestingly enough, while my Windows 10 would fail between flags 1 and 2, my Windows 7 fails between flags 2 and 3, and returns too a 0xC0000005 error. But again, only occasionally.

c
windows
asked on Stack Overflow Apr 12, 2019 by sointaminn • edited Apr 13, 2019 by Robert

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0