ImageProgressive crash: "pointer being freed was not allocated"
Check List
Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.
- [X] I have read the wiki page and cheat sheet, but there is no information I need.
- [X] I have searched in existing issues, but did not find a same one.
- [x] I want to report a problem instead of asking a question. It'd better to use kingfisher tag in Stack Overflow to ask a question.
Issue Description
A common crash in our logs comes from this library. In production we currently have version 5.7.1, but the problems seems still to be in the latest version.
Taking a look at the code, I believe this is caused because the items array is being manipulated in one queue and read in another. Swift arrays are not thread safe.
Stacktrace
From version 5.7.1
0 libsystem_kernel.dylib 0x33776defc __pthread_kill
1 libsystem_pthread.dylib 0x364c51d0c pthread_kill
2 libsystem_c.dylib 0x1b0362a70 abort
3 libsystem_malloc.dylib 0x1b03c3f88 malloc_vreport
4 libsystem_malloc.dylib 0x1b03c4150 malloc_report
5 libsystem_malloc.dylib 0x1b03b80dc free
6 libswiftCore.dylib 0x367b96c08 _swift_release_dealloc
7 libswiftCore.dylib 0x367b95a70 swift_release
8 libswiftDispatch.dylib 0x38e6dbaec OS_dispatch_queue.init
9 Kingfisher 0x105137b2c ImageProgressiveSerialQueue.init (ImageProgressive.swift:265)
10 Kingfisher 0x1051394f8 [inlined] init
11 Kingfisher 0x1051394f8 ImageProgressiveProvider.init (ImageProgressive.swift:78)
12 Kingfisher 0x105182050 [inlined] init
13 Kingfisher 0x105182050 [inlined] init (ImageProgressive.swift:80)
14 Kingfisher 0x105182050 KingfisherWrapper<T>.setImage (ImageView+Kingfisher.swift:113)
15 Kingfisher 0x1051836b0 KingfisherWrapper<T>.setImage (ImageView+Kingfisher.swift:204)
16 ... our code calling setImage
According to the log, the crash happens when initializing the ImageProgressiveProvider, and especially, when initializing a DispatchQueue value in ImageProgressiveSerialQueue. It should be totally a thread-safe behavior, and the problem is more likely under Swift or the system.
We could have a way to try to rewrite the code (such as adding an explicit initializer to ImageProgressiveSerialQueue), to see if we can work around it, but actually it should not be an issue in Kingfisher framework. :(