简体   繁体   English

C++/libpng - fclose() 在调用 png_write_png() 后“触发断点”

[英]C++/libpng - fclose() “triggers a breakpoint” after a call to png_write_png()

I remade a function I found in a tutorial for testing purposes, where I just fill a 512 by 512 canvas with pixels of different colors and save that to a file.我重新制作了一个 function 我在一个教程中找到用于测试目的,我只是用不同 colors 的像素填充一个 512 x 512 canvas 并保存到文件中。 Then the title happens.然后标题发生了。 Should I comment the call to png_write_png() out, no breakpoints are left in sight.我是否应该注释掉对png_write_png()的调用,看不到断点。

int save_test_png()
{
    const string path = "D:\\test.png";
    const size_t X = 512, Y = 512;
    const size_t PIXEL_SIZE = 4;
    const size_t BIT_DEPTH = 8;

    FILE* image_file_ptr;
    png_structp png_ptr = nullptr;
    png_infop info_ptr = nullptr;
    png_byte** row_pointers = nullptr;

    int status = 0;

    if (fopen_s(&image_file_ptr, path.c_str(), "wb"))
    {
        return -1;
    }

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
    {
        if (image_file_ptr)
        {
            fclose(image_file_ptr);
        }
        return -2;
    }

    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        if (image_file_ptr)
        {
            fclose(image_file_ptr);
        }
        return -3;
    }

    if (setjmp(png_jmpbuf(png_ptr)))
    {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        if (image_file_ptr)
        {
            fclose(image_file_ptr);
        }
        return -4;
    }

    png_set_IHDR(png_ptr,
        info_ptr,
        X, Y,
        BIT_DEPTH,
        PNG_COLOR_TYPE_RGBA,
        PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_DEFAULT,
        PNG_FILTER_TYPE_DEFAULT);

    row_pointers = static_cast<png_byte**>(png_malloc(png_ptr, Y * sizeof(png_byte*)));
    for (size_t y = 0; y < Y; y++)
    {
        png_byte* row = static_cast<png_byte*>(png_malloc(png_ptr, sizeof(png_byte) * X * PIXEL_SIZE));
        row_pointers[y] = row;
        for (size_t x = 0; x < X; x++)
        {
            *row++ = (x & 0x3F) << 2;                           // red
            *row++ = ((x & 0x1C0) >> 1) | ((y & 0x1C0) >> 4);   // green
            *row++ = (y & 0x3F) << 2;                           // blue
            *row++ = 0xFF;                                      // alpha
        }
    }

    png_init_io(png_ptr, image_file_ptr);
    png_set_rows(png_ptr, info_ptr, row_pointers);
    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);

    status = 0;

    for (size_t y = 0; y < Y; y++)
    {
        png_free(png_ptr, row_pointers[y]);
    }
    png_free(png_ptr, row_pointers);
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(image_file_ptr);

    return status;
}

I built libpng and zlib from source, using the Visual Studio solutions that came with the official releases.我使用官方版本附带的 Visual Studio 解决方案从源代码构建了 libpng 和 zlib。 The project is currently configured as Debug x64, .lib and.dll files for libpng and zlib were built in Release x64 configuration as suggested in the manual.该项目当前配置为 Debug x64,.lib 和 .dll 文件 libpng 和 zlib 按照手册中的建议在 Release x64 配置中构建。 "AppName.exe triggered a breakpoint" also disappears if I reconfigure the project I'm working on as Release x64.如果我将正在处理的项目重新配置为 Release x64,“AppName.exe 触发断点”也会消失。

The "triggered a breakpoint" exception disappeared when I linked my Debug x64 configuration to.lib files built under a Debug x64 configuration and also placed a.dll file built under the same configuration in the project's x64/Debug folder, where the executable file is located.当我将我的 Debug x64 配置链接到在 Debug x64 配置下构建的.lib 文件并将在相同配置下构建的 .dll 文件放置在项目的 x64/Debug 文件夹中时,“触发断点”异常消失了,其中可执行文件位于位于。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM