I'm creating a map with n * m dimensions, that should be gotten from a file input. Each position of the map matters for distance counting. In the whole execution cycle I go through 4 malloc() calls, however I can't match them with 4 free() calls or the execution might or might not finish and I can't understand why.
Here's the main call:
int main()
{
map_t newMap;
newMap = map_create(30, 30);
//there's some test calls that I'm ommiting as they simply work and don't matter
map_destroy(newMap);
return 0;
}
the map_t as I understand and as I want is a pointer to a structure, it's defined on a header file as following:
typedef struct map *map_t;
it's implementation is on the corresponding .c file for matters of encapsulation. Here's the implementation and the important functions for the case:
struct map{
position_t **positions;
int lines, columns;
};
map_t map_create(int n, int m)
{
map_t newMap = malloc(sizeof(map_t));
newMap->lines = n;
newMap->columns = m;
newMap->positions = malloc(n * sizeof(position_t*));
int i;
int k;
for(i = 0; i < n; ++i)
{
newMap->positions[i] = malloc(m * sizeof(position_t));
for(k = 0; k < m; ++k)
{
newMap->positions[i][k] = position_create();
}
}
return newMap;
}
void map_destroy(map_t map_p)
{
int i, k;
int n = map_p->lines;
int m = map_p->columns;
for(i=0; i < n; ++i)
{
for(k=0; k < m; ++k)
{
position_destroy(map_p->positions[i][k]);
}
free(map_p->positions[i]);
}
free(map_p->positions);
free(map_p);
}
For matters of completeness, here's the position struct specific code, it follows the same idea of the map struct.
typedef struct position *position_t; //this is on the .h, the rest is on the .c file
struct position{
int has_treasure;
int times_visited;
};
position_t position_create()
{
position_t newPosition = malloc(sizeof *newPosition);
newPosition->has_treasure = YES;
newPosition->times_visited = 0;
return newPosition;
}
void position_destroy(position_t position_p)
{
free(position_p);
}
I've identified which free calls are causing the problem, I just can't understand why. If I comment these 2 lines:
free(map_p->positions);
free(map_p);
everything works fine. I've seen different ways to allocate the initial memory such as this one - How do I correctly set up, access, and free a multidimensional array in C? - but even with the various solutions found there I keep getting the same unstability.
It might be extremely simple and I'm just overlooking something, been on this for 5 days now, as much I hate to borrow anyone's time on this mess, even if I have to implement a different solution I'd like to at least understand what's wrong.
Code isn't producing any warnings with -Wall -g -c and if I add -pedantic I just get complaints about comments.
Cheers in advance
EDIT: Changed position_t newPosition = malloc(sizeof(position_t));
to position_t newPosition = malloc(sizeof *newPosition);
behaviour is still the same
EDIT2: did the exact type of change on the map_t malloc, problem solved, the whole time I thought the problem was on the **positions