Toucan icon indicating copy to clipboard operation
Toucan copied to clipboard

Crash in CGImageWithCorrectOrientation

Open AvdLee opened this issue 7 years ago • 2 comments
trafficstars

We currently expire crashes in our app, which we "fixed" by removing force unwraps and adding logging to get more insights.

Crashlog

Crashed: com.apple.root.user-interactive-qos
0  Rabbit                         0x104977fc4 specialized static Toucan.Util.CGImageWithCorrectOrientation(UIImage) -> CGImage (Toucan.swift:571)
1  Rabbit                         0x10497871c specialized static Toucan.Resize.resizeImage(UIImage, size : CGSize, fitMode : Toucan.Resize.FitMode) -> UIImage? (Toucan.swift)
2  Rabbit                         0x1049c9100 specialized ThumbnailGenerator.generate(for : UIImage) -> [ThumbnailGenerator.GeneratedThumbnail] (ThumbnailGenerator.swift:61)
3  Rabbit                         0x1049fa134 specialized ContentMetadataFetcher.(generateVisualPreviews(for : Content, image : UIImage, storageController : LocalStorageController, completionHandler : ([VisualPreview]) -> ()) -> ()).(closure #2).(closure #1) (ContentMetadataFetcher+Thumbnails.swift:73)
4  Rabbit                         0x1049fa08c partial apply for ContentMetadataFetcher.(generateVisualPreviews(for : Content, image : UIImage, storageController : LocalStorageController, completionHandler : ([VisualPreview]) -> ()) -> ()).(closure #2).(closure #1) (ContentMetadataFetcher+Thumbnails.swift)
5  Rabbit                         0x10493db00 thunk (ContentCreationController.swift)
6  libdispatch.dylib              0x1851a6a54 _dispatch_call_block_and_release + 24
7  libdispatch.dylib              0x1851a6a14 _dispatch_client_callout + 16
8  libdispatch.dylib              0x1851b3ea4 _dispatch_root_queue_drain + 1032
9  libdispatch.dylib              0x1851b3a38 _dispatch_worker_thread3 + 120
10 libsystem_pthread.dylib        0x18544f06c _pthread_wqthread + 1268
11 libsystem_pthread.dylib        0x18544eb6c start_wqthread + 4

Device and system information

iOS 10 and iOS 11 iPad and iPhone

Known stats from logging

The crash is happening on this line which force unwraps the CGContext. It seems that scenarios exist in which the created context is nil.

After adding a guard and logging in the return statement, we've seen logs like this:

Error Domain=Toucan Code=-3 "width: 3840 height: 5760 bitsPerComponent: 8 bitmapInfo: 5" UserInfo={NSLocalizedDescription=width: 3840 height: 5760 bitsPerComponent: 8 bitmapInfo: 5}
Error Domain=Toucan Code=-3 "width: 120 height: 160 bitsPerComponent: 8 bitmapInfo: 0" UserInfo={NSLocalizedDescription=width: 120 height: 160 bitsPerComponent: 8 bitmapInfo: 0}

I've tried to reproduce the crash by hardcoding these values, but that didn't reproduce the crash. I've also made sure that the cgImage and Colorspace are non nil:

guard let cgImage = image.cgImage else {
    throw NSError(domain: "Toucan", code: -1, userInfo: [NSLocalizedDescriptionKey: "CGImage is nil"])
}

guard let colorSpace = cgImage.colorSpace else {
    throw NSError(domain: "Toucan", code: -2, userInfo: [NSLocalizedDescriptionKey: "Color space is nil"])
}

We've never seen these being logged.

Next steps

We for now prevented this crash from happening again by adding guards and throws instead of force unwrapping. It would be great if we can find the cause of this crash and fix it instead.

AvdLee avatar Apr 05 '18 08:04 AvdLee

Hi @AvdLee, I've been trying to reproduce this without much luck. Indeed, like you said, adding a guard for CGContext failing would silently fix the issue, but I'm interested in getting to the root cause, otherwise you'd end up with empty images (which isn't great).

Do you have any sample images on which this fails with? Another possibility is if it's being used on a table cell that's being reaped? There was a recent PR merged (#68) to fix those types of issues.

gavinbunney avatar Jan 07 '19 21:01 gavinbunney

Hi @gavinbunney, thanks for taking the time to look into this.

Unfortunately, we didn't have any images reproducing this issue. On top of that, we're no longer using Toucan so this issue is no longer causing any issues on our side. This makes it hard to give any additional information on whether it's fixed or still occurring.

Let me know if you have any other questions and I see what I can do.

AvdLee avatar Jan 08 '19 10:01 AvdLee