dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

Crash in Async Operation #146

Closed greese closed 3 years ago

greese commented 3 years ago

I think there's a bug in here in ImageDecoder.swift:

public func createFrameImage(at index: Int, subsamplingLevel: SubsamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage? {

... case .asynchronous: // Don't consider the subsamplingLevel when comparing the image native size with sizeForDrawing. guard var size = frameSize(at: index) else { return nil }

            if let sizeForDrawing = decodingOptions.sizeForDrawing {
                // See which size is smaller: the image native size or the sizeForDrawing.
                if sizeForDrawing.width * sizeForDrawing.height < size.width * size.height {
                    size = sizeForDrawing
                }
            }

            options = imageSourceAsyncOptions(sizeForDrawing: size, subsamplingLevel: subsamplingLevel)
            **image = CGImageSourceCreateThumbnailAtIndex(imageSource, index, options)**

...

I am getting an occasional crash here and it appears to occur when there is reloading images from the cache. If I felt more confident in my Swift, I'd wrap that bolded line in a DispatchQueue.main.async { } block as I think what is happening is that it's trying to update the UI from outside the main queue. Maybe?

In particular, it seems this code is running inside the DiskCache decode queue.

Screen Shot 2021-04-22 at 19 39 27

As with any async bug based on reloading junk from cache, this is a pain to actually make happen. But the most common scenario is when I am navigating from one view to a previously viewed view with a lot of images in it.

dmytro-anokhin commented 3 years ago

Hey, thank you for reporting. This looks like thread explosion, what explains why you encounter it when there are many images. Which device/OS version you running?

greese commented 3 years ago

tvOS, both in simulator and on device (Apple 4k for both). It's a free app in the App Store called Sparq Festival Platform if it helps to look at it.

It does load a lot of images. Basically, it's a streaming platform and we have rows of lazyhstacks forming a carousel of content on each page.

dmytro-anokhin commented 3 years ago

Hey, version 2.2.5 limits number of concurrent operations with disk cache. This should solve the problem.

greese commented 3 years ago

Thank you!

— George Reese @.*** +1.612.991.4446

On Wednesday, Apr 28, 2021 at 13:41, Dmytro Anokhin @. @.)> wrote:

Hey, version 2.2.5 limits number of concurrent operations with disk cache. This should solve the problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub (https://github.com/dmytro-anokhin/url-image/issues/146#issuecomment-828691007), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABZI5WKH72V4CUWO7NZUE3TLBJGJANCNFSM43NUBYLQ).