iDevGames Forums
libPng And Memory Access Violations - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: libPng And Memory Access Violations (/thread-3468.html)

libPng And Memory Access Violations - Jaden - Feb 23, 2007 12:55 PM

I'm having some problems using libpng to read png files. I can open the file, read the header and see that it's definitely a png file, but as soon as I call png_read_png ( which in turn calls fread and attempts to read memory I'm assuming it's not supposed to ) I get memory access errors causing my program to crash.

I've stepped through a million times and can't seem to find what I'm doing wrong. And do to the definite lack of simple libpng examples, I don' have much to compare this against. (Other then the libpng manual but that all seems to agree with what's here.)

I used this program as the basis for my read png function:

void pngLoad::read_png_file(const char *file_name)
    png_byte header[8];

    FILE *fp = fopen(file_name, "rb");
    if (!fp)
        std::cout << "[read_png_file] File %s could not be opened for reading";
    fread(header, 1, 8, fp);
    bool is_png = !png_sig_cmp(header, 0, 8);
    if (!is_png)
        std::cout << "[read_png_file] File %s is not recognized as a PNG file";

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
        std::cout << "[read_png_file] png_create_read_struct failed";

    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
        std::cout << "[read_png_file] png_create_info_struct failed";

    if (setjmp(png_jmpbuf(png_ptr)))
        std::cout << "[read_png_file] Error during init_io";

    png_init_io(png_ptr, fp);
    png_set_sig_bytes(png_ptr, 8);

    png_read_png(png_ptr, info_ptr, NULL, NULL);

    /* The rest of the function that is never reached

libPng And Memory Access Violations - Skorche - Feb 23, 2007 02:05 PM

My PNG loading code looks like this:
#include <stdlib.h>
#include <assert.h>

#include "png.h"

#define PNG_SIG_BYTES 8

char *
load_png(char *name, int *width, int *height)
    FILE *png_file = fopen(name, "rb");

    uint8_t header[PNG_SIG_BYTES];

    fread(header, 1, PNG_SIG_BYTES, png_file);
    assert(!png_sig_cmp(header, 0, PNG_SIG_BYTES));

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

    png_infop info_ptr = png_create_info_struct(png_ptr);

    png_infop end_info = png_create_info_struct(png_ptr);

    png_init_io(png_ptr, png_file);
    png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
    png_read_info(png_ptr, info_ptr);

    *width = png_get_image_width(png_ptr, info_ptr);
    *height = png_get_image_height(png_ptr, info_ptr);

    png_uint_32 bit_depth, color_type;
    bit_depth = png_get_bit_depth(png_ptr, info_ptr);
    color_type = png_get_color_type(png_ptr, info_ptr);
    if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)

    if (bit_depth == 16)
    if(color_type == PNG_COLOR_TYPE_PALETTE)
    else if(color_type == PNG_COLOR_TYPE_GRAY ||
            color_type == PNG_COLOR_TYPE_GRAY_ALPHA)

    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

    png_read_update_info(png_ptr, info_ptr);

    png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
    png_uint_32 numbytes = rowbytes*(*height);
    png_byte* pixels = malloc(numbytes);
    png_byte** row_ptrs = malloc((*height) * sizeof(png_byte*));

    int i;
    for (i=0; i<(*height); i++)
      row_ptrs[i] = pixels + ((*height) - 1 - i)*rowbytes;

    png_read_image(png_ptr, row_ptrs);

    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);

    return (char *)pixels;

I do a lot of unnecessary steps in between, but I would expect that maybe you are needing the png_read_info() call. I'd bet that is required to initialize the structs properly. I could look it up in the docs, but I'm feeling lazy at the moment.

libPng And Memory Access Violations - Jaden - Feb 23, 2007 02:15 PM

Well, originally I had it going the "low-level" way and started off with png_read_info(). png_read_png() is supposed to let you go around that.

Ironically though, png_read_info() gave me the same issue when I had it in there. With the memory violation and what not.

I'll go back to trying it with png_read_info. And thank you for the source, I'll look it over and see if I can find the difference.

P.S. Your post count of 666 is making me wonder if I should trust your advice Rasp

libPng And Memory Access Violations - Jaden - Feb 23, 2007 02:48 PM

Hmm... my code is almost identical to yours up to the png_read_info(png_ptr, info_ptr);

But it crashes there.

Maybe the problem is outside my code? Something in the png file perhaps?

libPng And Memory Access Violations - OneSadCookie - Feb 23, 2007 03:31 PM

Mine is here:

And I don't have exactly 666 posts Rasp

libPng And Memory Access Violations - Skorche - Feb 23, 2007 04:21 PM

Hah! There.

Now I can be trusted. Wink

libPng And Memory Access Violations - Jaden - Feb 23, 2007 04:43 PM

Well, it looks like I've finally pin down the error, and as I suspected, it wasn't in the source code itself.

Believe it or not, it's a windows specific issue...

...what, no one is surprised?

Quote:So you pass the FILE* from fopen to png_init_io which will cause libpng
itself to make fread() calls on that FILE*. If libpng then crashes in
png_read_info it means that the library you used to allocate the FILE* (i.e.
the one containing the implementation of fopen which was called in (1)) is
*not* the same as the library libpng expects to call to read from the FILE*
(i.e. the library was compiled/linked against to get the
implementation of fread).

If you can"t change your library to match libpng and you don"t want to
compile and build libpng (statically) to change it then you have no choice
but to implement your own file IO and hope that there are no other issues (I
believe that file IO with stdio is the *only* case where an object is
allocated in the application and subject to library calls within libpng.)

Now if only I better understood his fix...because I really don't feel like writing my own file IO.

libPng And Memory Access Violations - Jaden - Feb 23, 2007 08:14 PM

Well, for those of you who absolutely could not sleep until you found out what came of this problem, it's fixed now. Turned out the libpng version I was using was from 2004...*sigh* So I downloaded the newest source and recompiled the libraries. Lo and behold, it works now. Thanks for all the help.

libPng And Memory Access Violations - AnotherJake - Feb 23, 2007 11:01 PM

Skorche Wrote:Hah! There.

Now I can be trusted. Wink

Continuing the way OT: I did 666 last week too. Didn't see any black cats crossing my path or anything either. I guess 666 is the number right after 665 and just before 667 after all. Whew! Sneaky

RE: libPng And Memory Access Violations - miye - Sep 3, 2013 04:50 AM

I have encountered the same problem as you.
And my version of libpng is 1.2.37, can you tell me which version should I
use? Or can you send your libpng to me. My e-mail is
Many thanks.

RE: libPng And Memory Access Violations - AnotherJake - Sep 3, 2013 07:10 AM

This is a pretty old thread, so I don't know if the OP is still around to answer. Personally I haven't used libpng in ages. Lately I've been using stb_image for loading pngs. If you don't need any png features other than just loading pngs for a game, it works great and is easy to use.

RE: libPng And Memory Access Violations - ThemsAllTook - Sep 3, 2013 02:13 PM

I've encountered libpng crashes caused by linking to a mismatched version of zlib. Make sure you're linking against the exact zlib version that your libpng states it's compatible with; in my case, I had to downgrade from zlib 1.2.5 to 1.2.3 to get it to work without crashing with libpng 1.4.1.

RE: libPng And Memory Access Violations - miye - Sep 4, 2013 04:50 AM

How to solve this problem? I have tried many different methods. However, I still can't find a solution, any one can help me 。