I'm writing a PNG loader using libpng, and I've run up against a strange problem. I've written the code correctly, so far as I can see from the example file and the tutorial provided, and it compiles fine, but it crashes when I call png_read_info(png_ptr, info_ptr);
The error occurs in the file _file.c, and has something to do with locking the file, as far as I can see. The exact message is:
Unhandled exception at 0x7c918fea in ImageTest.exe: 0xC0000005: Access violation writing location 0x00000010.
My code:
FILE *fpPNG = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_byte sig[8];
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
fpPNG = fopen(sFilename.c_str(), "rb");
if(!fpPNG) throw Exception("The Portable Network Graphics file \"" + sFilename + "\" could not be opened.", __FUNCTION__, __FILE__, __LINE__);
try
{
// Read 8 bytes of the file, and check them...
fread(sig, 1, sizeof(png_byte [8]), fpPNG);
if(!png_check_sig(sig, 8)) throw Exception("The Portable Network Graphics file \"" + sFilename + "\" is not valid.", __FUNCTION__, __FILE__, __LINE_);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr)NULL, (png_error_ptr)NULL);
if(png_ptr == NULL) throw Exception("An error has occurred reading the Portable Network Graphics file \"" + sFilename + "\".", __FUNCTION__, __FILE__, __LINE__);
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL) throw Exception("An error has occurred reading the Portable Network Graphics file \"" + sFilename + "\".", __FUNCTION__, __FILE__, __LINE__);
png_init_io(png_ptr, fpPNG);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr); // <- This is the problem...
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
png_bytep *row_pointers = new png_bytep[height];
for(png_uint_32 row = 0; row < height; row++)
{
row_pointers[row] = (png_bytep)png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
}
catch(Exception e)
{
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fpPNG);
fpPNG = NULL;
throw e;
}
fclose(fpPNG);
fpPNG = NULL;
Anyone got any suggestions? I've tried both the debug and release builds of the libpng/zlib DLL, and tested them with the pngtest program. They both work, apparently.