Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libpng crashes on png_write_into (Windows 10, VS2013, self-built, all tests pass ok)

I'm observing a crash due to an access violation in libpng (1.6.20) when calling png_write_info. I have built libpng from source (including zlib 1.2.8) and the png tests that come with the libpng source code all pass ok without any errors. I can confirm that good png files are being created during these tests.

The simple breakdown of my program (until where it crashes) looks like this. I have removed all error and bounds checking for simplicity reasons:

int main(int argc, char *argv[]) {
    char* filename = argv[1];
    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_infop info = png_create_info_struct(png);
    if (setjmp(png_jmpbuf(png)))
    {
        abort();
    }

    // Output is 16-bit depth, greyscale format.
    png_set_IHDR(
        png,
        info,
        127, 127,
        16,
        PNG_COLOR_TYPE_GRAY,
        PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_DEFAULT,
        PNG_FILTER_TYPE_DEFAULT
    );

    // do the file stuff
    FILE *fp;
    errno_t error = fopen_s(&fp, filename, "wb");
    png_init_io(png, fp);
    png_write_info(png, info); // <-- crashes here with "access violation writing location ..."

Thanks for your help!

codeSourcerer

like image 932
codeSourcerer Avatar asked Oct 31 '22 13:10

codeSourcerer


1 Answers

Ok, I got the answer to this one thanks to another question posted here: libpng crashes on png_read_info()

In there the answer is given by:

If you don't use the Visual Studio defaults your application must still be built with the default runtime option (/MD). If, for some reason, it is not then your application will crash inside libpng16.dll as soon as libpng tries to read from a file handle you pass in.

So I recompiled my libpng with the properties->Configuration Properties->C/C++->Code Generation->Runtime Library set to Multi-Threaded DLL (/MD) (not the debug one!!) and set the same for my png-writer project. Now it's working and does not crash anymore.

Cheers,
codeSourcerer

like image 97
codeSourcerer Avatar answered Nov 15 '22 04:11

codeSourcerer