const int pixel_size = 4; // RGBA
png_bytep *row_pointers =
png_malloc(pPngStruct, height * sizeof(png_byte *));
+ if (!row_pointers) {
+ png_destroy_write_struct(&pPngStruct, &pPngInfo);
+ fclose(fp);
+ return;
+ }
unsigned int *blocks = (unsigned int *)data;
int y = 0;
for (; y < height; ++y) {
png_bytep row =
png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
+ if (!row) {
+ for (x = 0; x < y; x++)
+ png_free(pPngStruct, row_pointers[x]);
+ png_free(pPngStruct, row_pointers);
+ png_destroy_write_struct(&pPngStruct, &pPngInfo);
+ fclose(fp);
+ return;
+ }
+
row_pointers[y] = (png_bytep)row;
for (x = 0; x < width; ++x) {
unsigned int curBlock = blocks[y * width + x];