Skip to content

Instantly share code, notes, and snippets.

@pburleson
Created May 24, 2012 22:44
Show Gist options
  • Save pburleson/2784669 to your computer and use it in GitHub Desktop.
Save pburleson/2784669 to your computer and use it in GitHub Desktop.
UIImagePNGRepresentation Crash
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3602632c __pthread_kill + 8
1 libsystem_c.dylib 0x323de29f abort + 94
2 ImageIO 0x37eca8bf png_error + 114
3 ImageIO 0x37ec9fe3 png_write_end + 46
4 ImageIO 0x37ec6069 writeOnePng + 2260
5 ImageIO 0x37ec578b _CGImagePluginWritePNG + 82
6 ImageIO 0x37ec56fd CGImageDestinationFinalize + 132
7 UIKit 0x37342fa7 UIImagePNGRepresentation + 274
Note: Image data is not nil.
@pburleson
Copy link
Author

The exception code:

Exception Type: SIGABRT
Exception Codes: #0 at 0x3602632c
Crashed Thread: 0

@JensAyton
Copy link

It looks like the CG PNG plug-in is either failing to call png_set_error_fn(), or using an error handler which returns (instead of longjmping away or throwing an exception). It is then issuing an invalid sequence of PNG writing calls. Libpng is built with setjmp support disabled, so the default error handler aborts.

Presumably not every call to UIImagePNGRepresentation() triggers this crash, so there’s probably something about your usage that’s triggering the invalid sequence (for instance, a zero-sized image might lead to no IDAT chunks being written, which seems to be the only call to png_error() in png_write_end() itself).

Tl;dr: the specific crash is an Apple bug, but even if they fixed it it must be triggered by some problem on your end.

@dlinsin
Copy link

dlinsin commented May 25, 2012

We had problems with UIImagePNGRepresentation related to corrupted images, downloaded from a web server. You could identify the corrupted images, by trying to look at them in Safari.

@RestlessThinker
Copy link

Did you ever find the root of this problem or a solution for this? I'm having the same crash. The images could be downloaded from a web server. I'm thinking of checking the first 8 bytes to confirm it's a PNG but I'm not sure if a zero-sized image would contain this or not.

@reidmain
Copy link

I'm having this same problem. Attempts to create "bad" UIImages and serialize them using UIImagePNGRepresentation have proved fruitless. My current attempt to figure out what is causing this call to png_error() is create a text file with all the URLs of every possible image in our app and then create a simple iOS app that downloads every image and then attempts to serialize it. I hope this will point out which images are corrupt then allow us to fix it as well as put in some preventative code that will prevent this crash from happening in the case that someone uploads a corrupted image again.

I'll post back with my progress.

@reidmain
Copy link

Unfortunately after downloading 7500+ images I experienced no errors.

One interesting note is that the crash reports are only for iOS 5.1+.

@leftspin
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment