I feel that a piece of code I have would cause a memory leak. I have a data structure with two two-dimensional arrays, one containing ints and one containing pointers to dynamically-allocated objects (sprites). The data structure is a tilemap, and the ints are the numeric index of each location, which are read from a file. I call that index ’tiles’. This tells what kind of tile it is, for behavioral purposes (i.e. player responds differently to water than to dirt or ice). The objects are the sprites to draw at their respective locations. That index is known as ‘images’. That index tells the tilemap what sprite to draw at that position.
typedef struct
{
int** tiles;
sprite*** images;
int w, h;
} tilemap;
I have a function that creates a new tilemap, initializes it, and returns it.
tilemap* new_tilemap(int w, int h, const char* filename)
{
tilemap* tm = malloc(sizeof(tilemap));
tm->w = w;
tm->h = h;
/*allocate memory space for the tiles index*/
tm->tiles = malloc(sizeof(int) * h);
int i, j;
for (i = 0; i < h; ++i)
{
tm->tiles[i] = malloc(sizeof(int) * w);
}
/*fill the index with the appropriate data from a file*/
FILE* file = fopen (filename, "rb");
if (file == NULL)
{
printf("Failed to open map %s\n", filename);
}
for (j = 0; j < h; ++j)
{
for (i = 0; i < w; ++i)
{
fscanf(file, "%d", &(tm->tiles[j][i]));
}
}
fclose(file);
/*allocate space for the images*/
tm->images = malloc(sizeof(sprite*) * h);
for (i = 0; i < h; ++i)
{
tm->images[i] = malloc(sizeof(sprite*) * w);
}
/*load images based on what type of tile is at that position*/
for (j = 0; j < h; ++j)
{
for (i = 0; i < w; ++i)
{
switch (tm->tiles[j][i])
{
case 0:
tm->images[j][i] = new_sprite_file("dat/tiles/0.bmp", 1);
break;
case 1:
tm->images[j][i] = new_sprite_file("dat/tiles/1.bmp", 2);
break;
}
tm->images[j][i]->x = i*tm->images[j][i]->w;
tm->images[j][i]->y = j*tm->images[j][i]->h;
}
}
return tm;
}
Then, to free the tilemap and all it’s structures I have this function:
void free_tilemap(tilemap* tm)
{
/*loop through and free each of the images in the array*/
int i, j;
for (j = 0; j < tm->h; ++j)
{
for (i = 0; i < tm->w; ++i)
{
free(tm->images[j][i]);
}
}
/*free the actual array*/
free(tm->images);
/*free the tile array?*/
free(tm->tiles);
/*free the entire tilemap structure*/
free(tm);
}
However, I feel that it isn’t freeing up all the memory I have allocated, because I used malloc twice on the tiles, but only free’d once. I don’t know if this is a problem though, seeing as they are ints, but I think that I may have to loop through the tiles array, free every row, then loop through and free every column (containing the rows), in the same manner as it was allocated. Is that what needs to be done or am I just being ignorant and/or paranoid? The same with the images array. Also, feel free to point out other flaws in my code as I know I’m not the best programmer.
Of course you should mirror the
mallocs when you free.Same goes for the other
fors that closely resemble this one. Freeing justtilesdoesn’t automatically freetiles[0..h]in cascade.