Description
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.