I am wondering why this little peace of code is leaking memory? It is flipping my image horizontally and returning the struct Image to my main file.
void turnImg(Image *img) {
Image *tmp = (Image*)malloc(sizeof(Image));
//swapping size between height and width
tmp->height = img->height;
img->height = img->width;
img->width = tmp->height;
//The loop is swapping every pixel from the "last" pixel to the "first" into a tmp
tmp->pixels = (Pixel**)malloc(sizeof(Pixel*)*img->height);
for (unsigned int i = 0; i < img->height; i++) {
tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*img->width);
for (unsigned int j = 0; j < img->width; j++) {
tmp->pixels[i][j] = img->pixels[img->width - 1 - j][img->height - 1 - i];
}
}
for (unsigned i = 0; i < img->height; i++) {
free(img->pixels[i]);
}
free(img->pixels);
//tmp gives back the pixels to img, but they are now flipped
img->pixels = tmp->pixels;
free(tmp);
}
Nothing should be wrong in the main file because all of my other functions are doing fine... But here is the peace of the main which is sending and returning the struct to the function:
case 5:
//Flipping the image on its diagonal.
printf("Flipping the image on its diagonal...");
turnImg(&plain);
printf("Image flipped.\n");
break;
And the main file ends with:
for (unsigned int i = 0; i < plain.height; i++) {
free(plain.pixels[i]);
}
free(plain.pixels);
getchar();
return 0;
However, what I have noticed is that the height and width swap is a part of the problem, but I do not how I will be able to do this without a swap.