Port Max's crash fix to my 3.6 fork

This is the original PR.
https://github.com/rs/SDWebImage/pull/662/files