c-为什么printf在运行png_read_png函数后不打印



平台=Win10x64编译器=GCCLang=2库=1libpng16.dll

我正在尝试使用libpng库,但printf在这一行之后什么也不输出:

png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);

完整代码:

#include <stdio.h>
#include <png.h>
int main(int argc, char* argv[])
{
printf("starting...n");

FILE *fp = fopen("test.png", "rb");
if (!fp) {
printf("error #%dn", 1);
}  
// Read the PNG header
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
printf("error #%dn", 2);
}
// create stuct for png info/meta data
png_infop info_ptr = png_create_info_struct(png_ptr);

if (!info_ptr) {
printf("error #%dn", 3);
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("error #%dn", 4);
}    
png_init_io(png_ptr, fp);
// Read the PNG image data
printf("before printf diesn");
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
printf("this will not print after png_read_png is rann");
fflush(stdout);
/* it won't print regardless if the below cleanup code is commented out or not */
// Get the image dimensions and bit depth
png_uint_32 width, height;
int bit_depth, color_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);

// Allocate memory for the image data
png_bytep *row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep));
for (png_uint_32 i = 0; i < height; i++)
{
row_pointers[i] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
}
// Read the image data into memory
png_read_image(png_ptr, row_pointers);
// Clean up
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

// Free the memory allocated for the image data
for (png_uint_32 i = 0; i < height; i++)
{
png_free(png_ptr, row_pointers[i]);
}
png_free(png_ptr, row_pointers);
/**/
fclose(fp);

return 0;
}

它将输出以下内容:

starting...
before printf dies

抱歉问新手这个问题。但我找不到这方面的任何信息。只是一堆帖子说用换行符结束printf,但我正在这么做。提前感谢您的帮助!

解释为什么您的代码看起来是锁定的而不是进度的,是因为您误解了setjmp()返回的内容。你似乎认为它返回0表示成功,而且!0表示"0";错误";。

这不正确。当您实际调用setjmp()时,它会返回0,并且!0表示当其他代码调用longjmp()并返回到那里时。

png_read_image()似乎(出于某种原因)正在调用jongjmp()以发出错误信号。因此,您的代码进入了一个无限循环。

所有的错误测试不应该只记录错误然后继续程序。它们应该退出()或返回。

显然还有其他一些问题(png_read_image()失败的原因),但这是对当前问题(代码锁定)的解释。

经过大量调查,png_read_png似乎遇到了一个无法报告的错误(即使使用了setjmp和自定义错误捕获)。出现这种情况的原因似乎是源代码和与源代码一起打包的二进制代码之间不兼容。因此,如果不从src编译libpng并创建自己的二进制文件,目前无法在Windows 10上使用C编写libpng程序,除非你能找到提供兼容源代码的Windows x64二进制文件的第三方。

回顾printf不打印的原因:

  1. png_read_png由于未知src/binary不匹配而遇到意外情况
  2. png_read_png无法调用自定义错误处理程序,并在调用png_read.png之前跳转到setjpm点
  3. png_read_png被再次调用,循环继续
  4. Windows必须以某种方式检测到一个无休止的循环,然后程序悄悄退出,看起来好像它在没有打印的情况下完成了,但从未真正到达较低的printf语句
  5. 利润

相关内容

最新更新