[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

libpng - Access Violation - png_read_info

Posted on 2005-04-23
9
Medium Priority
?
2,440 Views
Last Modified: 2012-08-13
I'm trying to read a PNG file with libpng, but I'm getting an Access Violation crash on png_read_info and I can't figure out why... am I forgeting something ?

[CODE]
int loadPNG()
{
      png_structp            png_ptr = NULL;
      png_infop            info_ptr = NULL;
      png_infop            endinfo_ptr = NULL;
      unsigned char      header[8];

      png_uint_32 width, height;
      int depth, color;
      
      FILE *fp = fopen(strTexturePath, "rb");
      if (fp == NULL) return FALSE;
      
      fread(header, 1, 8, fp);
      if (!png_check_sig(header, 8)) return FALSE;

      png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
      if (!png_ptr)
            return FALSE;
      info_ptr = png_create_info_struct(png_ptr);
      if (!info_ptr)
      {
            png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
            return FALSE;
      }
      endinfo_ptr = png_create_info_struct(png_ptr);
      if (!endinfo_ptr)
      {
            png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
            return FALSE;
      }

      if (setjmp(png_ptr->jmpbuf))
      {
            png_destroy_read_struct(&png_ptr, &info_ptr, &endinfo_ptr);
            return FALSE;
      }
      
      png_init_io(png_ptr, fp);
      png_set_sig_bytes(png_ptr, 8);
      png_read_info(png_ptr, info_ptr); /* Line of the crash */
      png_get_IHDR(png_ptr, info_ptr, &width, &height, &depth, &color, NULL, NULL, NULL);

      /* [...] */
      png_read_end(png_ptr, endinfo_ptr);
      png_destroy_read_struct(&png_ptr, &info_ptr, &endinfo_ptr);

      return TRUE;
}
[/CODE]
0
Comment
Question by:WysG
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 13852439
You have to allocate some memory in the info_ptr, as png_read_info takes that as an input parameter.
0
 
LVL 8

Expert Comment

by:_corey_
ID: 13852697
He has allocated it properly.

However, I don't know what the setjmp is for.  The PNG loader I wrote uses the read/write function pointer setup.

I just call the png_read_info() and then png_get_IHDR and it works fine with the same style of png_ptr and info_ptr setup.

Can you run this in debug, and step through png_read_info and find out where it actually crashes?  Also, tell me what the setjmp is for.

corey
0
 
LVL 22

Expert Comment

by:grg99
ID: 13853617
Sorry, I'm not currently at a computer with the right stuff on it to debug this code.

You'll find it very worthwhile to install a debugger on your system, and learn it thoroughly.  WIll save you days of flailing around.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13855754
I believe the code is based the 'manual', here: http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3

Are you sure the png file is valid?  Try viewing it in your browser.

Are any error messages being printed?

Where did you get your libpng?  Did you compile it yourself, are you using a binary distribution, or what?
0
 
LVL 3

Author Comment

by:WysG
ID: 13855914
Novadenizen is right, the code is based on the online manual

The png is a valid png file and I'm able to view it

I got my libpng from the bin distribution on the libpng site (I'm using the dll) : http://sourceforge.net/project/showfiles.php?group_id=23617&package_id=16183&release_id=287509

I'm checking the source of libpng, in pngread.c line 306 :
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_read_info(png_structp png_ptr, png_infop info_ptr)
{ /* [...] */ }

Could it be that by default, the bin distribution is set with PNG_NO_SEQUENTIAL_READ_SUPPORTED ?

> Also, tell me what the setjmp is for.
According to the manual it's some kind of error handling function

0
 
LVL 8

Expert Comment

by:_corey_
ID: 13856007
Can you debug and tell us where it is crashing?

corey
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 480 total points
ID: 13856320
Maybe you should compile your own from source.  There may be some kind of incompatibility between their compiler and your compiler.
0
 
LVL 8

Accepted Solution

by:
_corey_ earned 520 total points
ID: 13858708
Well, I doubt there's a compile consistancy issue, but then again I don't know what exactly his setup is.

You should be able to step-through and debug anyway if you have a debug build of the library, you'll just need to point Visual Studio to the source code when you step-in.

However, compiling from source is very simple, just make a png directory, put all of their code in that directory.  You should remove the test file (pngtest.c?) and just add that whole directory to your project.

corey
0
 
LVL 3

Author Comment

by:WysG
ID: 13863048
Thanks _corey_ and NovaDenizen,
It seems that the dll don't include the function png_read_info, that's why my program was crashing and why I couldn't step into it to see where in the function the problem was. So I droped the dll and include the source of libpng and zlib in my project and I now I can read the info from the png, thanks guys
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question