alexiscreuzot / SwiftyGif

High performance GIF engine
MIT License
1.99k stars 210 forks source link

Fix: Crash when gif loaded from url and delegate has been deallocated #158

Closed nuno-vieira closed 2 years ago

nuno-vieira commented 2 years ago

Description

Hello 👋 First of all, thank you for open sourcing this library 🙂

We have some customers reporting this crash, and we were able to reproduce it and fix it 🙂 Here is the crash log: Crash.txt

How to reproduce

It is a bit hard to reproduce this one. In our case, this happens because sometimes we make a call to setGifFromURL right before the view is deallocated. So one way to try and reproduce this is to call setGifFromURL on a deinit lifecycle of an object.

Cause

The URLSession completion block retains self which prolongs the image view lifetime even if its superview and delegate have been deallocated already. When accessing the unsafe delegate, the app crashes.

Solution

By not retaining self on the URLSession completion block, and weakly referencing it, it won't crash. I noticed that the same is happening on NSImageView so I applied the same fix.

nuno-vieira commented 2 years ago

Possibly this is related to this reported crash: https://github.com/kirualex/SwiftyGif/issues/143 since our crash is also on that line.

nuno-vieira commented 2 years ago

Sorry for the ping @kirualex. Could you give an estimation on when this can be merged and released?

Thank you!